User Tools

Site Tools


Syntax of Regular Expressions

Simple matches

Any single character matches itself, unless it is a meta-character 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 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.


foobar          matches string 'foobar'
\^FooBarPtr     matches '^FooBarPtr' 

Escape sequences

Characters may be specified using a escape sequences syntax much like that used in C and Perl: '\n' matches a newline, '\t' a tab, etc. More generally, \xnn, where nn is a string of hexadecimal digits, matches the character whose ASCII value is nn. If You need wide (Unicode) character code, You can use '\x{nnnn}', where 'nnnn' - one or more hexadecimal digits.

\xnn     char with hex code nn
\n       newline (NL), same as \x0a
\r       car.return (CR), same as \x0d
\f       form feed (FF), same as \x0c
\e       escape (ESC), same as \x1b


foo\x20bar   matches 'foo bar' (note space in the middle)
\tfoobar     matches 'foobar' predefined by tab

Character classes


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.



[-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 char from ']'..'a'.


Meta-characters are special characters which are the essence of Regular Expressions. There are different types of meta-characters, described below.

Line separators

^      start of line
$      end of line
\A     start of text
\Z     end of text
.      any character in line


^foobar     matches string 'foobar' only if it's at the beginning of line
foobar$     matches string 'foobar' only if it's at the end of line
^foobar$    matches string 'foobar' only if it's the only string in 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 '$'. You may, however, wish to treat a string as a multi-line buffer, such that the '^' will match after any line separator within the string, and '$' will match before any line separator. You can do this by switching On the modifier /m. 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.

Predefined classes

\w     an alphanumeric character (including "_")
\W     a nonalphanumeric
\d     a numeric character
\D     a non-numeric
\S     a non space



foob\dr     matchs strings like 'foob1r', 'foob6r' and so on but not 'foobar', 'foobbr' and so on

Word boundaries

\b     Match a word boundary
\B     Match a non-(word boundary)



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

  {n}    exactly n times ("greedy")




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'

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


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


foo(bar|foo)  matches strings 'foobar' or 'foofoo'.


The bracketing construct ( … ) may also be used for define r.e. subexpressions.

Subexpressions are numbered based on the left to right order of their opening parenthesis. First subexpression has number '1'.


(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 \1 through \9 are interpreted as backreferences. \<n> matches previously matched subexpression #<n>.


(.)\1+         matches 'aaaa' and 'cc'.
(['"]?)(\d+)\1 matches "13" (in double quotes), or '4' (in single quotes) or 77 (without quotes) etc
regular_expressions.txt · Last modified: 2016/04/20 13:59 (external edit)

Page Tools