Syntax of Regular
Expressions
Regular expression looks ugly for novices, but really it's very
simple, handly and powerfull tool.
Some examples:
Any heading line:
(?i)<h[\d]*>
real number (examples '13.88e-4', '-7E2'):
([+\-]?\d+(\.\d+)?([eE][+\-]?\d+)?)
phone number (examples '+7(812) 555-5555', '(20)555-55-55',
'555-5555'):
((\+\d *)?(\(\d{2,4}\)
*)?\d{3}(-\d*)*)
e-mail address (examples 'anso@mail.com',
'anso@mailbox.alkor.com'):
([_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+)
Internet URL (examples 'http://www.paycash.com',
'ftp://195.5.138.172\default.htm'):
([Ff][Tt][Pp]|[Hh][Tt][Tt][Pp])://([_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+))((/[
_a-zA-Z\d\-\\\.]+)+)*
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.
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. If you need wide (UniCode) character code,
you can use '\x{nnnn}', where 'nnnn' - one or more hexadedimal
digits.
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.
[\d-t] match any digit, '-' or 't'. []-a] match any char from
']'..'a'.
Finally, the ``.'' metacharacter matches any character except
``\n'' (unless you use /s modifier - see below. Note: /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 mathing 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.
Subexpressions are numbered based on the left to right order of
their opening parenthesis.
First subexpression has number '1'
Any item of a regular expression may be followed with digits in
curly brackets 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.
Short list of metacharacters
| ^
|
- start of line |
| $
|
- end of line |
| .
|
- any character |
| \
|
- quote next character |
| *
|
- match zero or more, similar to {0,} |
| +
|
- match one or more, similar to {1,} |
| ?
|
- match zero or one, similar to {0,1} |
| {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 thru 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 |
| \1 ..
\9
|
- backreferences |
You may use \w, \d and \s within character classes.
\1 through \9 are interpreted as backreferences. \<n> matches
previously matched subexpression #<n>. For example: '(.)\1+'
match 'aaaa' and 'cc'. '(.+)\1+' also match 'abab' and '123123'.
See examples in demo project
By default, the ^ character is only guaranteed to match at the
beginning of the string, the $ character only at the end (or before
the newline at the end) and perl does certain optimizations with
the assumption that the string contains only one line. Embedded
newlines 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 newline within the string,
and ``$'' will match before any newline. At the cost of a little
more overhead, you can do this by using the m modifier on the
pattern match operator.
To facilitate multi-line substitutions, the ``.'' character never
matches a newline unless you use the s modifier.
List of modifiers
| i
|
Do case-insensitive pattern matching (using installed in you
system locale settings). |
| s
|
Treat string as single line. That is, change ``.'' to match any
character whatsoever, even a newline, which it normally would not
match.
|
| r
|
Non-standard modifier. If modifier 'r' is set then range à -ÿ
additional include russian letter '¸', À-ß additional include '¨',
and à -ß include all russian symbols. Sorry for foreign users, but
it's set by default. |
Perl extensions
(?imsxr-imsxr)
You may use it into r.e. for modifying modifiers by the fly, for
example
(?i)New York
will match string 'New york' and 'New York', but
(?i)New (?-i)York
will match only 'New York'
If this construction inlined into subexpression, then it effects
only into this subexpression
(?i)(New )?York
will match 'New york' and 'new york' , but
((?i)New )?York
will match 'new York', but not 'new york'
(?#text)
A comment. The text is ignored. If the x switch is used to enable
whitespace formatting, a simple # will suffice.
|