### 25.1 Regular expressions ¶

Regular expressions are a formal method for describing a certain class of mathematical languages. They may be viewed as patterns which match some set of strings. They are very common in many software tools such as scripting languages like the UNIX shell, PERL, awk, Emacs etc. Unfortunately the exact form of regualr expressions often differs slightly between different applications making their use often a little tricky.

Festival support regular expressions based mainly of the form used in the GNU libg++ `Regex` class, though we have our own implementation of it. Our implementation (`EST_Regex`) is actually based on Henry Spencer’s `regex.c` as distributed with BSD 4.4.

Regular expressions are represented as character strings which are interpreted as regular expressions by certain Scheme and C++ functions. Most characters in a regular expression are treated as literals and match only that character but a number of others have special meaning. Some characters may be escaped with preceding backslashes to change them from operators to literals (or sometime literals to operators).

`.`

  Matches any character.

`$`

  matches end of string

`^`

  matches beginning of string

`X*`

  matches zero or more occurrences of X, X may be a character, range of parenthesized expression.

`X+`

  matches one or more occurrences of X, X may be a character, range of parenthesized expression.

`X?`

  matches zero or one occurrence of X, X may be a character, range of parenthesized expression.

`[...]`

  a ranges matches an of the values in the brackets. The range operator "-" allows specification of ranges e.g. `a-z` for all lower case characters. If the first character of the range is `^` then it matches anything character except those specified in the range. If you wish `-` to be in the range you must put that first.

`\\(...\\)`

  Treat contents of parentheses as single object allowing operators `*`, `+`, `?` etc to operate on more than single characters.

`X\\|Y`

  matches either X or Y. X or Y may be single characters, ranges or parenthesized expressions.

Note that actuall only one backslash is needed before a character to escape it but because these expressions are most often contained with Scheme or C++ strings, the escape mechanaism for those strings requires that backslash itself be escaped, hence you will most often be required to type two backslashes.

Some example may help in enderstanding the use of regular expressions.

`a.b`

  matches any three letter string starting with an `a` and ending with a `b`.

`.*a`

  matches any string ending in an `a`

`.*a.*`

  matches any string containing an `a`

`[A-Z].*`

  matches any string starting with a capital letter

`[0-9]+`

  matches any string of digits

`-?[0-9]+\\(\\.[0-9]+\\)?`

  matches any positive or negative real number. Note the optional preceding minus sign and the optional part contain the point and following numbers. The point itself must be escaped as dot on its own matches any character.

`[^aeiouAEIOU]+`

  mathes any non-empty string which doesn’t conatin a vowel

`\\([Ss]at\\(urday\\)\\)?\\|\\([Ss]un\\(day\\)\\)`

  matches Saturday and Sunday in various ways

The Scheme function `string-matches` takes a string and a regular expression and returns `t` if the regular expression macthes the string and `nil` otherwise.