Previous Up Next

16  Regular Expressions

This description is taken from the documentation to the TRegExpr Unit from Andrej V. Sorokin1, Copyright © 1999.

Regular expression looks ugly for novices, but really it's very simple (well, usually simple ;) ), handy and powerfull tool.

16.1  Some examples

16.2  Detailed explanation

Any single character matches itself, unless it is a metacharacter with a special meaning described below.

A series of characters matches that series of characters in the target string, so the pattern "bluh" would match "bluh" in the target string. Quite simple eh ?

You can cause characters that normally function as metacharacters to be interpreted literally by prefixing them with a "\". For example, "^" match beginning of string, but "\^" match character "^", "\\" match "\" and so on.

You can specify a character class, by enclosing a list of characters in [], which will match any one character from the list. If the first character after the "[" is "^", the class matches any character not in the list. Within a list, the "-" character is used to specify a range, so that a-z represents all characters between "a" and "z", inclusive. If you want "-" itself to be a member of a class, put it at the start or end of the list, or escape it with a backslash.

The following all specify the same class of three characters: [-az], [az-], and [a\-z]. All are different from [a-z], which specifies a class containing twenty-six characters. If you want `]' you may place it at the start of list or escape it with a backslash. Examples of queer ;) ranges: [\n- \x0D] match any of #10,#11,#12,#13. [\-t] match any digit, `-' or `t'. []-a] match any char from ']'..'a'.

Characters may be specified using a metacharacter syntax much like that used in C: "\n" matches a newline, "\t" a tab, "\r" a carriage return, "\f" a form feed, etc. More generally, \xnn, where nn is a string of hexadecimal digits, matches the character whose ASCII value is nn.

Finally, the "." metacharacter matches any character except "\n" (unless you use /s modifier - see below. Note: in TRegExpr /s is set by default).

You can specify a series of alternatives for a pattern using "|" to separate them, so that fee|fie|foe will match any of "fee", "fie", or "foe" in the target string (as would f(e|i|o)e). The first alternative includes everything from the last pattern delimiter ("(", "[", or the beginning of the pattern) up to the first "|", and the last alternative contains everything from the last "|" to the next pattern delimiter. For this reason, it's common practice to include alternatives in parentheses, to minimize confusion about where they start and end.

Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when matching foo|foot against "barefoot", only the "foo" part will match, as that is the first alternative tried, and it successfully matches the target string. (This might not seem important, but it is important when you are capturing matched text using parentheses.)

Also remember that "|" is interpreted as a literal within square brackets, so if you write [fee|fie|foe] you're really only matching [feio|].

The bracketing construct ( ...) may also be used for define r.e. subexpressions (after parsing you may find subexpression positions, lengths and actual values in MatchPos, MatchLen and Match properties of TRegExpr, and substitute it in template strings by TRegExpr.Substitute). Subexpressions are numbered based on the left to right order of their opening parenthesis.

First subexpression has number `1' (whole r.e. match has number `0' - you may substitute it in TRegExpr.Substitute as `$0' or `$&').

Any item of a regular expression may be followed with digits in curly brackets (now it implemented in TRegExpr only for simple cases. If you want to use curly brackets for subexpressions, please. remove `.' from {.$DEFINE ComplexBraces} in regexpr.pas, but note - it's not properly tested) of the form {n,m}, where n gives the minimum number of times to match the item and m gives the maximum. The form {n} is equivalent to {n,n} and matches exactly n times. The form {n,} matches n or more times. (If a curly bracket occurs in any other context, it is treated as a regular character.) The * modifier is equivalent to {0,}, the + modifier to {1,} and the ? modifier to {0,1}. There is no limit to the size of n or m, but large numbers will chew up more memory and slow down r.e. execution.
Character Meaning
^ start of line
$ end of line
. any character
\ quote next character
* match zero or more
+ match one or more
{n} Match exactly n times
{n,\} Match at least n times
{n,m} Match at least n but not more than m times
[aeiou0-9] match a, e, i, o, u, and 0 through 9 ;
[^aeiou0-9] match anything but a, e, i, o, u, and 0 thru 9
\w matches an alphanumeric character (including "_")
\W a nonalphanumeric
\d matches a numeric character
\D a non-numeric
\s matches any space (same as [ \t\n\r\f])
\S a non space


Table 3: Metacharacters for Regular Expressions




1
The used Library is from Andrej V. Sorokin, and you can download it at: http://anso.da.ru/. He translated some C code to Delphi and improved it to create the TRegExpr Unit.

Previous Up Next