2007-11-07 18:26:11 +00:00
|
|
|
[/
|
|
|
|
Copyright 2006-2007 John Maddock.
|
|
|
|
Distributed under the Boost Software License, Version 1.0.
|
|
|
|
(See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
http://www.boost.org/LICENSE_1_0.txt).
|
|
|
|
]
|
|
|
|
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[section:perl_syntax Perl Regular Expression Syntax]
|
|
|
|
|
|
|
|
[h3 Synopsis]
|
|
|
|
|
|
|
|
The Perl regular expression syntax is based on that used by the
|
|
|
|
programming language Perl . Perl regular expressions are the
|
2009-04-25 17:32:49 +00:00
|
|
|
default behavior in Boost.Regex or you can pass the flag =perl= to the
|
2007-06-08 09:13:34 +00:00
|
|
|
[basic_regex] constructor, for example:
|
|
|
|
|
|
|
|
// e1 is a case sensitive Perl regular expression:
|
|
|
|
// since Perl is the default option there's no need to explicitly specify the syntax used here:
|
|
|
|
boost::regex e1(my_expression);
|
|
|
|
// e2 a case insensitive Perl regular expression:
|
|
|
|
boost::regex e2(my_expression, boost::regex::perl|boost::regex::icase);
|
|
|
|
|
|
|
|
[h3 Perl Regular Expression Syntax]
|
|
|
|
|
|
|
|
In Perl regular expressions, all characters match themselves except for the
|
|
|
|
following special characters:
|
|
|
|
|
|
|
|
[pre .\[{()\\\*+?|^$]
|
|
|
|
|
|
|
|
[h4 Wildcard]
|
|
|
|
|
|
|
|
The single character '.' when used outside of a character set will match
|
|
|
|
any single character except:
|
|
|
|
|
|
|
|
* The NULL character when the [link boost_regex.ref.match_flag_type flag
|
2009-04-25 17:32:49 +00:00
|
|
|
=match_not_dot_null=] is passed to the matching algorithms.
|
2007-06-08 09:13:34 +00:00
|
|
|
* The newline character when the [link boost_regex.ref.match_flag_type
|
2009-04-25 17:32:49 +00:00
|
|
|
flag =match_not_dot_newline=] is passed to
|
2007-06-08 09:13:34 +00:00
|
|
|
the matching algorithms.
|
|
|
|
|
|
|
|
[h4 Anchors]
|
|
|
|
|
|
|
|
A '^' character shall match the start of a line.
|
|
|
|
|
|
|
|
A '$' character shall match the end of a line.
|
|
|
|
|
|
|
|
[h4 Marked sub-expressions]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
A section beginning =(= and ending =)= acts as a marked sub-expression.
|
2007-06-08 09:13:34 +00:00
|
|
|
Whatever matched the sub-expression is split out in a separate field by
|
|
|
|
the matching algorithms. Marked sub-expressions can also repeated, or
|
|
|
|
referred to by a back-reference.
|
|
|
|
|
|
|
|
[h4 Non-marking grouping]
|
|
|
|
|
|
|
|
A marked sub-expression is useful to lexically group part of a regular
|
|
|
|
expression, but has the side-effect of spitting out an extra field in
|
|
|
|
the result. As an alternative you can lexically group part of a
|
|
|
|
regular expression, without generating a marked sub-expression by using
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?:= and =)= , for example =(?:ab)+= will repeat =ab= without splitting
|
2007-06-08 09:13:34 +00:00
|
|
|
out any separate sub-expressions.
|
|
|
|
|
|
|
|
[h4 Repeats]
|
|
|
|
|
|
|
|
Any atom (a single character, a marked sub-expression, or a character class)
|
2009-04-25 17:32:49 +00:00
|
|
|
can be repeated with the =*=, =+=, =?=, and ={}= operators.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The =*= operator will match the preceding atom zero or more times,
|
|
|
|
for example the expression =a*b= will match any of the following:
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
b
|
|
|
|
ab
|
|
|
|
aaaaaaaab
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The =+= operator will match the preceding atom one or more times, for
|
|
|
|
example the expression =a+b= will match any of the following:
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
ab
|
|
|
|
aaaaaaaab
|
|
|
|
|
|
|
|
But will not match:
|
|
|
|
|
|
|
|
b
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The =?= operator will match the preceding atom zero or one times, for
|
2007-06-08 09:13:34 +00:00
|
|
|
example the expression ca?b will match any of the following:
|
|
|
|
|
|
|
|
cb
|
|
|
|
cab
|
|
|
|
|
|
|
|
But will not match:
|
|
|
|
|
|
|
|
caab
|
|
|
|
|
|
|
|
An atom can also be repeated with a bounded repeat:
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=a{n}= Matches 'a' repeated exactly n times.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=a{n,}= Matches 'a' repeated n or more times.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=a{n, m}= Matches 'a' repeated between n and m times inclusive.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
[pre ^a{2,3}$]
|
|
|
|
|
|
|
|
Will match either of:
|
|
|
|
|
|
|
|
aa
|
|
|
|
aaa
|
|
|
|
|
|
|
|
But neither of:
|
|
|
|
|
|
|
|
a
|
|
|
|
aaaa
|
|
|
|
|
|
|
|
It is an error to use a repeat operator, if the preceding construct can not
|
|
|
|
be repeated, for example:
|
|
|
|
|
|
|
|
a(*)
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
Will raise an error, as there is nothing for the =*= operator to be applied to.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h4 Non greedy repeats]
|
|
|
|
|
|
|
|
The normal repeat operators are "greedy", that is to say they will consume as
|
|
|
|
much input as possible. There are non-greedy versions available that will
|
|
|
|
consume as little input as possible while still producing a match.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=*?= Matches the previous atom zero or more times, while consuming as little
|
2007-06-08 09:13:34 +00:00
|
|
|
input as possible.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=+?= Matches the previous atom one or more times, while consuming as
|
2007-06-08 09:13:34 +00:00
|
|
|
little input as possible.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=??= Matches the previous atom zero or one times, while consuming
|
2007-06-08 09:13:34 +00:00
|
|
|
as little input as possible.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
={n,}?= Matches the previous atom n or more times, while consuming as
|
2007-06-08 09:13:34 +00:00
|
|
|
little input as possible.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
={n,m}?= Matches the previous atom between n and m times, while
|
2007-06-08 09:13:34 +00:00
|
|
|
consuming as little input as possible.
|
|
|
|
|
2009-04-23 09:51:31 +00:00
|
|
|
[h4 Pocessive repeats]
|
|
|
|
|
|
|
|
By default when a repeated patten does not match then the engine will backtrack until
|
|
|
|
a match is found. However, this behaviour can sometime be undesireable so there are
|
|
|
|
also "pocessive" repeats: these match as much as possible and do not then allow
|
|
|
|
backtracking if the rest of the expression fails to match.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=*+= Matches the previous atom zero or more times, while giving nothing back.
|
2009-04-23 09:51:31 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=++= Matches the previous atom one or more times, while giving nothing back.
|
2009-04-23 09:51:31 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=?+= Matches the previous atom zero or one times, while giving nothing back.
|
2009-04-23 09:51:31 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
={n,}+= Matches the previous atom n or more times, while giving nothing back.
|
2009-04-23 09:51:31 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
={n,m}+= Matches the previous atom between n and m times, while giving nothing back.
|
2009-04-23 09:51:31 +00:00
|
|
|
|
2007-06-08 09:13:34 +00:00
|
|
|
[h4 Back references]
|
|
|
|
|
|
|
|
An escape character followed by a digit /n/, where /n/ is in the range 1-9,
|
|
|
|
matches the same string that was matched by sub-expression /n/. For example
|
|
|
|
the expression:
|
|
|
|
|
|
|
|
[pre ^(a\*).\*\\1$]
|
|
|
|
|
|
|
|
Will match the string:
|
|
|
|
|
|
|
|
aaabbaaa
|
|
|
|
|
|
|
|
But not the string:
|
|
|
|
|
|
|
|
aaabba
|
2009-04-25 17:32:49 +00:00
|
|
|
|
|
|
|
You can also use the \g escape for the same function, for example:
|
|
|
|
|
|
|
|
[table
|
|
|
|
[[Escape][Meaning]]
|
|
|
|
[[=\g1=][Match whatever matched sub-expression 1]]
|
|
|
|
[[=\g{1}=][Match whatever matched sub-expression 1: this form allows for safer
|
|
|
|
parsing of the expression in cases like =\g{1}2= or for indexes higher than 9 as in =\g{1234}=]]
|
|
|
|
[[=\g-1=][Match whatever matched the last opened sub-expression]]
|
|
|
|
[[=\g{-2}=][Match whatever matched the last but one opened sub-expression]]
|
|
|
|
]
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h4 Alternation]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The =|= operator will match either of its arguments, so for example:
|
|
|
|
=abc|def= will match either "abc" or "def".
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
Parenthesis can be used to group alternations, for example: =ab(d|ef)=
|
2007-06-08 09:13:34 +00:00
|
|
|
will match either of "abd" or "abef".
|
|
|
|
|
|
|
|
Empty alternatives are not allowed (these are almost always a mistake), but
|
2009-04-25 17:32:49 +00:00
|
|
|
if you really want an empty alternative use =(?:)= as a placeholder, for example:
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=|abc= is not a valid expression, but
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?:)|abc= is and is equivalent, also the expression:
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?:abc)??= has exactly the same effect.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h4 Character sets]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
A character set is a bracket-expression starting with =[= and ending with =]=,
|
2007-06-08 09:13:34 +00:00
|
|
|
it defines a set of characters, and matches any single character that is a
|
|
|
|
member of that set.
|
|
|
|
|
|
|
|
A bracket expression may contain any combination of the following:
|
|
|
|
|
|
|
|
[h5 Single characters]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
For example =[abc]=, will match any of the characters 'a', 'b', or 'c'.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Character ranges]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
For example =[a-c]= will match any single character in the range 'a' to 'c'.
|
2007-06-08 09:13:34 +00:00
|
|
|
By default, for Perl regular expressions, a character x is within the
|
|
|
|
range y to z, if the code point of the character lies within the codepoints of
|
|
|
|
the endpoints of the range. Alternatively, if you set the
|
2009-04-25 17:32:49 +00:00
|
|
|
[link boost_regex.ref.syntax_option_type.syntax_option_type_perl =collate= flag]
|
2007-06-08 09:13:34 +00:00
|
|
|
when constructing the regular expression, then ranges are locale sensitive.
|
|
|
|
|
|
|
|
[h5 Negation]
|
|
|
|
|
|
|
|
If the bracket-expression begins with the ^ character, then it matches the
|
2009-04-25 17:32:49 +00:00
|
|
|
complement of the characters it contains, for example =[^a-c]= matches
|
|
|
|
any character that is not in the range =a-c=.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Character classes]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
An expression of the form [^\[\[:name:\]\]] matches the named character class
|
|
|
|
"name", for example [^\[\[:lower:\]\]] matches any lower case character.
|
2007-06-08 09:13:34 +00:00
|
|
|
See [link boost_regex.syntax.character_classes character class names].
|
|
|
|
|
|
|
|
[h5 Collating Elements]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
An expression of the form [^\[\[.col.\]\]] matches the collating element /col/.
|
2007-06-08 09:13:34 +00:00
|
|
|
A collating element is any single character, or any sequence of characters
|
|
|
|
that collates as a single unit. Collating elements may also be used
|
2009-04-25 17:32:49 +00:00
|
|
|
as the end point of a range, for example: [^\[\[.ae.\]-c\]] matches the
|
2007-06-08 09:13:34 +00:00
|
|
|
character sequence "ae", plus any single character in the range "ae"-c,
|
|
|
|
assuming that "ae" is treated as a single collating element in the current locale.
|
|
|
|
|
|
|
|
As an extension, a collating element may also be specified via it's
|
|
|
|
[link boost_regex.syntax.collating_names symbolic name], for example:
|
|
|
|
|
|
|
|
[[.NUL.]]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
matches a =\0= character.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Equivalence classes]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
An expression of the form [^\[\[\=col\=\]\]], matches any character or collating element
|
2007-06-08 09:13:34 +00:00
|
|
|
whose primary sort key is the same as that for collating element /col/, as with
|
|
|
|
collating elements the name /col/ may be a
|
|
|
|
[link boost_regex.syntax.collating_names symbolic name]. A primary sort key is
|
|
|
|
one that ignores case, accentation, or locale-specific tailorings; so for
|
|
|
|
example `[[=a=]]` matches any of the characters:
|
|
|
|
a, '''À''', '''Á''', '''Â''',
|
|
|
|
'''Ã''', '''Ä''', '''Å''', A, '''à''', '''á''',
|
|
|
|
'''â''', '''ã''', '''ä''' and '''å'''.
|
|
|
|
Unfortunately implementation of this is reliant on the platform's collation
|
|
|
|
and localisation support; this feature can not be relied upon to work portably
|
|
|
|
across all platforms, or even all locales on one platform.
|
|
|
|
|
|
|
|
[h5 Escaped Characters]
|
|
|
|
|
|
|
|
All the escape sequences that match a single character, or a single character
|
|
|
|
class are permitted within a character class definition. For example
|
|
|
|
`[\[\]]` would match either of `[` or `]` while `[\W\d]` would match any character
|
|
|
|
that is either a "digit", /or/ is /not/ a "word" character.
|
|
|
|
|
|
|
|
[h5 Combinations]
|
|
|
|
|
|
|
|
All of the above can be combined in one character set declaration, for example:
|
2009-04-25 17:32:49 +00:00
|
|
|
[^\[\[:digit:\]a-c\[.NUL.\]\]].
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h4 Escapes]
|
|
|
|
|
|
|
|
Any special character preceded by an escape shall match itself.
|
|
|
|
|
|
|
|
The following escape sequences are all synonyms for single characters:
|
|
|
|
|
|
|
|
[table
|
|
|
|
[[Escape][Character]]
|
2009-04-25 17:32:49 +00:00
|
|
|
[[=\a=][=\a=]]
|
|
|
|
[[=\e=][=0x1B=]]
|
|
|
|
[[=\f=][=\f=]]
|
|
|
|
[[=\n=][=\n=]]
|
|
|
|
[[=\r=][=\r=]]
|
|
|
|
[[=\t=][=\t=]]
|
|
|
|
[[=\v=][=\v=]]
|
|
|
|
[[=\b=][=\b= (but only inside a character class declaration).]]
|
|
|
|
[[=\cX=][An ASCII escape sequence - the character whose code point is X % 32]]
|
|
|
|
[[=\xdd=][A hexadecimal escape sequence - matches the single character whose
|
2007-06-08 09:13:34 +00:00
|
|
|
code point is 0xdd.]]
|
2009-04-25 17:32:49 +00:00
|
|
|
[[=\x{dddd}=][A hexadecimal escape sequence - matches the single character whose
|
2007-06-08 09:13:34 +00:00
|
|
|
code point is 0xdddd.]]
|
2009-04-25 17:32:49 +00:00
|
|
|
[[=\0ddd=][An octal escape sequence - matches the single character whose
|
2007-06-08 09:13:34 +00:00
|
|
|
code point is 0ddd.]]
|
2009-04-25 17:32:49 +00:00
|
|
|
[[=\N{name}=][Matches the single character which has the
|
2007-06-08 09:13:34 +00:00
|
|
|
[link boost_regex.syntax.collating_names symbolic name] /name/.
|
2009-04-25 17:32:49 +00:00
|
|
|
For example =\N{newline}= matches the single character \\n.]]
|
2007-06-08 09:13:34 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
[h5 "Single character" character classes:]
|
|
|
|
|
|
|
|
Any escaped character /x/, if /x/ is the name of a character class shall
|
|
|
|
match any character that is a member of that class, and any
|
|
|
|
escaped character /X/, if /x/ is the name of a character class, shall
|
|
|
|
match any character not in that class.
|
|
|
|
|
|
|
|
The following are supported by default:
|
|
|
|
|
|
|
|
[table
|
|
|
|
[[Escape sequence][Equivalent to]]
|
|
|
|
[[`\d`][`[[:digit:]]`]]
|
|
|
|
[[`\l`][`[[:lower:]]`]]
|
|
|
|
[[`\s`][`[[:space:]]`]]
|
|
|
|
[[`\u`][`[[:upper:]]`]]
|
|
|
|
[[`\w`][`[[:word:]]`]]
|
2009-04-23 09:51:31 +00:00
|
|
|
[[`\h`][Horizontal whitespace]]
|
|
|
|
[[`\v`][Vertical whitespace]]
|
2007-06-08 09:13:34 +00:00
|
|
|
[[`\D`][`[^[:digit:]]`]]
|
|
|
|
[[`\L`][`[^[:lower:]]`]]
|
|
|
|
[[`\S`][`[^[:space:]]`]]
|
|
|
|
[[`\U`][`[^[:upper:]]`]]
|
|
|
|
[[`\W`][`[^[:word:]]`]]
|
2009-04-23 09:51:31 +00:00
|
|
|
[[`\H`][Not Horizontal whitespace]]
|
|
|
|
[[`\V`][Not Vertical whitespace]]
|
2007-06-08 09:13:34 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
[h5 Character Properties]
|
|
|
|
|
|
|
|
The character property names in the following table are all equivalent
|
|
|
|
to the [link boost_regex.syntax.character_classes names used in character classes].
|
|
|
|
|
|
|
|
[table
|
|
|
|
[[Form][Description][Equivalent character set form]]
|
|
|
|
[[`\pX`][Matches any character that has the property X.][`[[:X:]]`]]
|
|
|
|
[[`\p{Name}`][Matches any character that has the property Name.][`[[:Name:]]`]]
|
|
|
|
[[`\PX`][Matches any character that does not have the property X.][`[^[:X:]]`]]
|
|
|
|
[[`\P{Name}`][Matches any character that does not have the property Name.][`[^[:Name:]]`]]
|
|
|
|
]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
For example =\pd= matches any "digit" character, as does =\p{digit}=.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Word Boundaries]
|
|
|
|
|
|
|
|
The following escape sequences match the boundaries of words:
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=\<= Matches the start of a word.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=\>= Matches the end of a word.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=\b= Matches a word boundary (the start or end of a word).
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=\B= Matches only when not at a word boundary.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Buffer boundaries]
|
|
|
|
|
|
|
|
The following match only at buffer boundaries: a "buffer" in this
|
|
|
|
context is the whole of the input text that is being matched against
|
|
|
|
(note that ^ and $ may match embedded newlines within the text).
|
|
|
|
|
|
|
|
\\\` Matches at the start of a buffer only.
|
|
|
|
|
|
|
|
\\' Matches at the end of a buffer only.
|
|
|
|
|
|
|
|
\\A Matches at the start of a buffer only (the same as \\\`).
|
|
|
|
|
|
|
|
\\z Matches at the end of a buffer only (the same as \\').
|
|
|
|
|
|
|
|
\\Z Matches an optional sequence of newlines at the end of a buffer:
|
2009-04-25 17:32:49 +00:00
|
|
|
equivalent to the regular expression =\n*\z=
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Continuation Escape]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The sequence =\G= matches only at the end of the last match found, or at
|
2007-06-08 09:13:34 +00:00
|
|
|
the start of the text being matched if no previous match was found.
|
|
|
|
This escape useful if you're iterating over the matches contained within a
|
|
|
|
text, and you want each subsequence match to start where the last one ended.
|
|
|
|
|
|
|
|
[h5 Quoting escape]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The escape sequence =\Q= begins a "quoted sequence": all the subsequent characters
|
2007-06-08 09:13:34 +00:00
|
|
|
are treated as literals, until either the end of the regular expression or \\E
|
2009-04-25 17:32:49 +00:00
|
|
|
is found. For example the expression: =\Q\*+\Ea+= would match either of:
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
\*+a
|
|
|
|
\*+aaa
|
|
|
|
|
|
|
|
[h5 Unicode escapes]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=\C= Matches a single code point: in Boost regex this has exactly the
|
2007-06-08 09:13:34 +00:00
|
|
|
same effect as a "." operator.
|
2009-04-25 17:32:49 +00:00
|
|
|
=\X= Matches a combining character sequence: that is any non-combining
|
2007-06-08 09:13:34 +00:00
|
|
|
character followed by a sequence of zero or more combining characters.
|
2009-04-25 17:32:49 +00:00
|
|
|
|
|
|
|
[h5 Matching Line Endings]
|
|
|
|
|
|
|
|
The escape sequence =\R= matches any line ending character sequence, specifically it is identical to
|
|
|
|
the expression [^(?>\x0D\x0A?|\[\x0A-\x0C\x85\x{2028}\x{2029}\])].
|
|
|
|
|
|
|
|
[h5 Keeping back some text]
|
|
|
|
|
|
|
|
=\K= Resets the start location of $0 to the current text position: in other words everything to the
|
|
|
|
left of \K is "kept back" and does not form part of the regular expression match. $` is updated
|
|
|
|
accordingly.
|
|
|
|
|
|
|
|
For example =foo\Kbar= matched against the text "foobar" would return the match "bar" for $0 and "foo"
|
|
|
|
for $`. This can be used to simulate variable width lookbehind assertions.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Any other escape]
|
|
|
|
|
|
|
|
Any other escape sequence matches the character that is escaped, for example
|
|
|
|
\\@ matches a literal '@'.
|
|
|
|
|
|
|
|
[h4 Perl Extended Patterns]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
Perl-specific extensions to the regular expression syntax all start with =(?=.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Comments]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?# ... )= is treated as a comment, it's contents are ignored.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Modifiers]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?imsx-imsx ... )= alters which of the perl modifiers are in effect within
|
2007-06-08 09:13:34 +00:00
|
|
|
the pattern, changes take effect from the point that the block is first seen
|
2009-04-25 17:32:49 +00:00
|
|
|
and extend to any enclosing =)=. Letters before a '-' turn that perl
|
2007-06-08 09:13:34 +00:00
|
|
|
modifier on, letters afterward, turn it off.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?imsx-imsx:pattern)= applies the specified modifiers to pattern only.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h5 Non-marking groups]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?:pattern)= lexically groups pattern, without generating an additional
|
2007-06-08 09:13:34 +00:00
|
|
|
sub-expression.
|
|
|
|
|
|
|
|
[h5 Lookahead]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
[^(?=pattern)] consumes zero characters, only if pattern matches.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?!pattern)= consumes zero characters, only if pattern does not match.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
Lookahead is typically used to create the logical AND of two regular
|
|
|
|
expressions, for example if a password must contain a lower case letter,
|
|
|
|
an upper case letter, a punctuation symbol, and be at least 6 characters long,
|
|
|
|
then the expression:
|
|
|
|
|
|
|
|
(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}
|
|
|
|
|
|
|
|
could be used to validate the password.
|
|
|
|
|
|
|
|
[h5 Lookbehind]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
[^(?<=pattern)] consumes zero characters, only if pattern could be matched
|
2007-06-08 09:13:34 +00:00
|
|
|
against the characters preceding the current position (pattern must be
|
|
|
|
of fixed length).
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?<!pattern)= consumes zero characters, only if pattern could not be
|
2007-06-08 09:13:34 +00:00
|
|
|
matched against the characters preceding the current position (pattern must
|
|
|
|
be of fixed length).
|
|
|
|
|
|
|
|
[h5 Independent sub-expressions]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?>pattern)= /pattern/ is matched independently of the surrounding patterns,
|
2007-06-08 09:13:34 +00:00
|
|
|
the expression will never backtrack into /pattern/. Independent sub-expressions
|
|
|
|
are typically used to improve performance; only the best possible match
|
|
|
|
for pattern will be considered, if this doesn't allow the expression as a
|
|
|
|
whole to match then no match is found at all.
|
|
|
|
|
|
|
|
[h5 Conditional Expressions]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?(condition)yes-pattern|no-pattern)= attempts to match /yes-pattern/ if
|
2007-06-08 09:13:34 +00:00
|
|
|
the /condition/ is true, otherwise attempts to match /no-pattern/.
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
=(?(condition)yes-pattern)= attempts to match /yes-pattern/ if the /condition/
|
2007-06-08 09:13:34 +00:00
|
|
|
is true, otherwise fails.
|
|
|
|
|
|
|
|
/condition/ may be either a forward lookahead assert, or the index of
|
|
|
|
a marked sub-expression (the condition becomes true if the sub-expression
|
|
|
|
has been matched).
|
|
|
|
|
|
|
|
[h4 Operator precedence]
|
|
|
|
|
|
|
|
The order of precedence for of operators is as follows:
|
|
|
|
|
|
|
|
# Collation-related bracket symbols `[==] [::] [..]`
|
2009-04-25 17:32:49 +00:00
|
|
|
# Escaped characters =\=
|
2007-06-08 09:13:34 +00:00
|
|
|
# Character set (bracket expression) `[]`
|
2009-04-25 17:32:49 +00:00
|
|
|
# Grouping =()=
|
|
|
|
# Single-character-ERE duplication =* + ? {m,n}=
|
2007-06-08 09:13:34 +00:00
|
|
|
# Concatenation
|
|
|
|
# Anchoring ^$
|
|
|
|
# Alternation |
|
|
|
|
|
|
|
|
[h3 What gets matched]
|
|
|
|
|
|
|
|
If you view the regular expression as a directed (possibly cyclic)
|
|
|
|
graph, then the best match found is the first match found by a
|
|
|
|
depth-first-search performed on that graph, while matching the input text.
|
|
|
|
|
|
|
|
Alternatively:
|
|
|
|
|
|
|
|
The best match found is the
|
|
|
|
[link boost_regex.syntax.leftmost_longest_rule leftmost match],
|
|
|
|
with individual elements matched as follows;
|
|
|
|
|
|
|
|
[table
|
|
|
|
[[Construct][What gets matched]]
|
2009-04-25 17:32:49 +00:00
|
|
|
[[=AtomA AtomB=][Locates the best match for /AtomA/ that has a following match for /AtomB/.]]
|
|
|
|
[[=Expression1 | Expression2=][If /Expresion1/ can be matched then returns that match,
|
2007-06-08 09:13:34 +00:00
|
|
|
otherwise attempts to match /Expression2/.]]
|
2009-04-25 17:32:49 +00:00
|
|
|
[[=S{N}=][Matches /S/ repeated exactly N times.]]
|
|
|
|
[[=S{N,M}=][Matches S repeated between N and M times, and as many times as possible.]]
|
|
|
|
[[=S{N,M}?=][Matches S repeated between N and M times, and as few times as possible.]]
|
|
|
|
[[=S?, S*, S+=][The same as =S{0,1}=, =S{0,UINT_MAX}=, =S{1,UINT_MAX}= respectively.]]
|
|
|
|
[[=S??, S*?, S+?=][The same as =S{0,1}?=, =S{0,UINT_MAX}?=, =S{1,UINT_MAX}?= respectively.]]
|
|
|
|
[[=(?>S)=][Matches the best match for /S/, and only that.]]
|
|
|
|
[[[^(?=S), (?<=S)]][Matches only the best match for /S/ (this is only
|
2007-06-08 09:13:34 +00:00
|
|
|
visible if there are capturing parenthesis within /S/).]]
|
2009-04-25 17:32:49 +00:00
|
|
|
[[=(?!S), (?<!S)=][Considers only whether a match for S exists or not.]]
|
|
|
|
[[=(?(condition)yes-pattern | no-pattern)=][If condition is true, then
|
2007-06-08 09:13:34 +00:00
|
|
|
only yes-pattern is considered, otherwise only no-pattern is considered.]]
|
|
|
|
]
|
|
|
|
|
|
|
|
[h3 Variations]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The [link boost_regex.ref.syntax_option_type.syntax_option_type_perl options =normal=,
|
|
|
|
=ECMAScript=, =JavaScript= and =JScript=] are all synonyms for
|
|
|
|
=perl=.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h3 Options]
|
|
|
|
|
|
|
|
There are a [link boost_regex.ref.syntax_option_type.syntax_option_type_perl
|
2009-04-25 17:32:49 +00:00
|
|
|
variety of flags] that may be combined with the =perl= option when
|
2007-06-08 09:13:34 +00:00
|
|
|
constructing the regular expression, in particular note that the
|
2009-04-25 17:32:49 +00:00
|
|
|
=newline_alt= option alters the syntax, while the =collate=, =nosubs= and
|
|
|
|
=icase= options modify how the case and locale sensitivity are to be applied.
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h3 Pattern Modifiers]
|
|
|
|
|
2009-04-25 17:32:49 +00:00
|
|
|
The perl =smix= modifiers can either be applied using a =(?smix-smix)=
|
2007-06-08 09:13:34 +00:00
|
|
|
prefix to the regular expression, or with one of the
|
|
|
|
[link boost_regex.ref.syntax_option_type.syntax_option_type_perl regex-compile time
|
2009-04-25 17:32:49 +00:00
|
|
|
flags =no_mod_m=, =mod_x=, =mod_s=, and =no_mod_s=].
|
2007-06-08 09:13:34 +00:00
|
|
|
|
|
|
|
[h3 References]
|
|
|
|
|
|
|
|
[@http://perldoc.perl.org/perlre.html Perl 5.8].
|
|
|
|
|
|
|
|
|
|
|
|
[endsect]
|
|
|
|
|
2007-11-07 18:26:11 +00:00
|
|
|
|