Regular Expression Syntax

Return to Introduction  Previous page  Next page

 

Regular Expressions is a widely used method of specifying patterns of text to search for. Special meta characters allow you to specify, for instance, that a particular string you are looking for occurs at the beginning or end of a line, or contains n recurrences of a certain character.

 

Simple matches

 

Any single character matches itself, unless it is a meta character with a special meaning.

 

A series of characters matches that series of characters in the target string, so the pattern "duck" would match "duck'' in the target string.

 

You can cause characters that normally function as meta characters or escape sequences to be interpreted literally by 'escaping' them by preceding them with a backslash "\", for instance: meta character "^" match beginning of string, but "\^" match character "^", "\\" match "\" and so on.

 

Examples:

 

duck

matches string 'duck'

\^duck

matches '^duck'

 

 

Escape sequences

 

Characters may be specified using a escape sequences syntax much like that used in programming and scripting languages C and Perl: "\n'' matches a new line, "\t'' a tab, etc. More generally, \xnn, where nn is a string of hexadecimal digits, matches the character whose ASCII value is nn.

 

\xnn

char with hex code nn

\x{nnnn}

char with hex code nnnn (one byte for plain text and two bytes for Unicode)

\t

tab (HT/TAB), same as \x09

\n

new line (LF), same as \x0a

\r

carriage return (CR), same as \x0d

\f

form feed (FF), same as \x0c

\a

alarm (bell) (BEL), same as \x07

\e

escape (ESC), same as \x1b

 

Examples:

 

du\x20ck

matches 'du ck' (note space in the middle)

\tduck

matches 'duck' predefined by tab

 

 

Character classes

 

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.

 

Examples:

 

foob[aeiou]r

matches strings 'foobar', 'foober' etc. but not 'foobbr', 'foobcr' etc.

foob[^aeiou]r

matches strings 'foobbr', 'foobcr' etc. but not 'foobar', 'foober' etc.

 

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. If you want ']' in the list you may place it at the start of list or escape it with a backslash.

 

Examples:

 

[-az]

matches 'a', 'z' and '-'

[az-]

matches 'a', 'z' and '-'

[a\-z]

matches 'a', 'z' and '-'

[a-z]

matches all twenty six small characters from 'a' to 'z'

[\n-\x0D]

matches any of #10,#11,#12,#13.

[\d-t]

matches any digit, '-' or 't'.

[]-a]

matches any character from ']'..'a'.

 

 

Meta characters

 

Meta characters are special characters which are the essence of Regular Expressions. There are different types of meta characters:

 

 

Meta characters - line separators

 

^

Start of line

$

End of line

\A

Start of text

\Z

End of text

.

Any character in line

 

Examples:

 

^duck

matches string 'duck' only if it's at the beginning of line

duck$

matches string 'duck' only if it's at the end of line

^duck$

matches string 'duck' only if it's the only string in the line

foob.r

matches strings like 'foobar', 'foobbr', 'foob1r' and so on

 

The "^" meta character by default is only guaranteed to match at the beginning of the input string/text, the "$" meta character only at the end. Embedded line separators will not be matched by "^'' or "$''.

 

The \A and \Z are just like "^'' and "$'', except that they won't match multiple times when the modifier /m is used, while "^'' and "$'' will match at every internal line separator.

 

The ".'' meta character by default matches any character, but if You switch Off the modifier /s, then '.' won't match embedded line separators.

 

"^" is at the beginning of a input string, and, if modifier /m is On, also immediately following any occurrence of \x0D\x0A or \x0A or \x0D. Note that there is no empty line within the sequence \x0D\x0A.

 

"$" is at the end of a input string, and, if modifier /m is On, also immediately preceding any occurrence of  \x0D\x0A or \x0A or \x0D. Note that there is no empty line within the sequence \x0D\x0A.

 

"." matches any character, but if You switch Off modifier /s then "." doesn't match \x0D\x0A and \x0A and \x0D.

 

Note that "^.*$" (an empty line pattern) does not match the empty string within the sequence \x0D\x0A, but matches the empty string within the sequence \x0A\x0D.

 

 

Meta characters - predefined classes

 

\w

an alphanumeric character (including "_")

\W

a non-alphanumeric character

\d

a numeric character

\D

a non-numeric character

\s

any space (same as [ \t\n\r\f])

\S

a non-space

 

You may use \w, \d and \s within custom character classes.

 

Examples:

 

foob\dr

matches strings like 'foob1r', ''foob6r' and so on but not 'foobar', 'foobbr' and so on

foob[\w\s]r

matches strings like 'foobar', 'foob r', 'foobbr' and so on but not 'foob1r', 'foob=r' and so on

 

 

Meta characters - iterators

 

Any item of a regular expression may be followed by another type of meta characters - iterators. Using this meta characters you can specify the number of occurrences of previous character, meta character or sub expression.

 

*

zero or more ("greedy"), similar to {0,}

+

one or more ("greedy"), similar to {1,}

?

zero or one ("greedy"), similar to {0,1}

{n}

exactly n times ("greedy")

{n,}

at least n times ("greedy")

{n,m}

at least n but not more than m times ("greedy")

*?

zero or more ("non-greedy"), similar to {0,}?

+?

one or more ("non-greedy"), similar to {1,}?

??

zero or one ("non-greedy"), similar to {0,1}?

{n}?

exactly n times ("non-greedy")

{n,}?

at least n times ("non-greedy")

{n,m}?

at least n but not more than m times ("non-greedy")

 

So, digits in curly brackets of the form {n,m}, specify the minimum number of times to match the item n and the maximum m. 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.

 

A little explanation about "greediness". "Greedy" takes as many as possible, "non-greedy" takes as few as possible. For example, 'b+' and 'b*' applied to string 'abbbbc' return 'bbbb', 'b+?' returns 'b', 'b*?' returns empty string, 'b{2,3}?' returns 'bb', 'b{2,3}' returns 'bbb'.

 

Examples:

 

foob.*r

matches strings like 'foobar',  'foobalkjdflkj9r' and 'foobr'

foob.+r

matches strings like 'foobar', 'foobalkjdflkj9r' but not 'foobr'

foob.?r

matches strings like 'foobar', 'foobbr' and 'foobr' but not 'foobalkj9r'

fooba{2}r

matches the string 'foobaar'

fooba{2,}r

matches strings like 'foobaar', 'foobaaar', 'foobaaaar' etc.

fooba{2,3}r

matches strings like 'foobaar', or 'foobaaar'  but not 'foobaaaar'

 

 

Meta characters - alternatives

 

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|].

 

Examples:

 

foo(bar|foo)

matches strings 'foobar' or 'foofoo'.

 

 

Meta characters - sub expressions

 

The bracketing construct ( ... ) may also be used for define sub-expressions. Sub expressions are numbered based on the left to right order of their opening parenthesis.

 

Examples:

 

(foobar){8,10}

matches strings which contain 8, 9 or 10 instances of the 'foobar'

foob([0-9]|a+)r

matches 'foob0r', 'foob1r' , 'foobar', 'foobaar', 'foobaar' etc.

 

 

Meta characters - back references

 

Meta characters \1 through \9 are interpreted as back references. \<n> matches previously matched sub expression #<n>.

 

Examples:

 

(.)\1+

matches 'aaaa' and 'cc'.

(.+)\1+

also matches 'abab' and '123123'

(['"]?)(\d+)\1

matches '"13" (in double quotes), or '4' (in single quotes) or 77 (without quotes) etc

 

 

Meta characters - word boundaries

 

\b

Matches a word boundary

\B

Matches a non-(word boundary)

 

A word boundary (\b) is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a \W.

 

 

Modifiers

 

Modifiers are for changing the behavior of the regular expression evaluation engine. Any of these modifiers may be embedded within the regular expression itself using the (?...) construct.

 

i

Do case-insensitive pattern matching

m

Treat string as multiple lines. That is, change "^'' and "$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string.

s

Treat string as single line. That is, change ".'' to match any character whatsoever, even a line separators, which it normally would not match.

 

Examples:

 

(?i)Saint-Petersburg

matches 'Saint-petersburg' and 'Saint-Petersburg'

(?i)Saint-(?-i)Petersburg

matches 'Saint-Petersburg' but not 'Saint-petersburg'

(?i)(Saint-)?Petersburg

matches 'Saint-petersburg' and 'saint-petersburg'

((?i)Saint-)?Petersburg

matches 'saint-Petersburg', but not 'saint-petersburg'

 

 

 

Most content on this page is Copyright (C) Andrey V. Sorokin

 



Back to index  Previous page  Next page