Regular Expressions

Top  Previous  Next

Regular expressions are used to search text using patterns. This section only documents the regular expressions as they are implemented in the GAMSIDE. We do not attempt to explain how regular expressions are used, but the simple examples provide a start.

 

Syntax for regular expression

 

<regular expression> ::= 

<expr> |

'^' <expr> |

<expr> '$' |

'^' <expr> '$'

 

<expr> ::= 

<term> |

<term> '|' <expr>                  // Alternation

 

<term> ::= 

<factor> |

<factor> <term>                    // Concatenation

 

<factor> ::= 

<atom> |

<atom> <iterator>

 

<atom> ::= 

<char> |

'(' <expr> ') |                    // Sub expression

'[' <charclass> ']' |              // Character class

'[^' <charclass> ']'               // Negated character class

 

<charclass> ::= 

<charrange> |

<charrange><charclass>

 

<charrange> ::= 

<ccchar> |

<ccchar> '-' <ccchar>              // Character range

 

<char> ::= 

<any character except meta characters> |

'\' <any character at all> |

<escape sequence>

 

<ccchar> ::= 

<any character except '-' and ']'> |

'\' <any character at all>

 

This syntax implies that parentheses have maximum precedence, followed by square brackets, followed by the closure operators, followed by concatenation, finally followed by alternation.

 

Meta characters                        

.

Any character

^

Start of line

$

End of line

\w

Alphanumeric including '_'

\W

Non alphanumeric character

\d

Numeric character

\D

Non numeric character

\s

Any space character

\S

Any non space character

\b

Word boundary

\B

Non word boundary

\1 thru \9

Back reference. \<n> matches previous matched sub-expression number <n>

 

Escape sequence

\xnn

Character with hex value nn

\t

Tab, same as \x09

\f

Formfeed, same as \x0c

\a

Bell, same as \x07

\e

Escape, same as \x1b

 

Iterator

Greedy

Non-greedy


*

*?

Zero or more; same as {0,}

+

+?

One or more; same as {1,}

?

??

Zero or one; same as {0,1}

{n}

{n}?

Exactly n times

{n,}

{n,}?

At least n times

{n,m}

{n,m}?

At least n but not more than m times