1[/ 2 Copyright 2006-2007 John Maddock. 3 Distributed under the Boost Software License, Version 1.0. 4 (See accompanying file LICENSE_1_0.txt or copy at 5 http://www.boost.org/LICENSE_1_0.txt). 6] 7 8 9[section:basic_extended POSIX Extended Regular Expression Syntax] 10 11[h3 Synopsis] 12 13The POSIX-Extended regular expression syntax is supported by the POSIX 14C regular expression API's, and variations are used by the utilities 15`egrep` and `awk`. You can construct POSIX extended regular expressions in 16Boost.Regex by passing the flag `extended` to the regex constructor, for example: 17 18 // e1 is a case sensitive POSIX-Extended expression: 19 boost::regex e1(my_expression, boost::regex::extended); 20 // e2 a case insensitive POSIX-Extended expression: 21 boost::regex e2(my_expression, boost::regex::extended|boost::regex::icase); 22 23[#boost_regex.posix_extended_syntax][h3 POSIX Extended Syntax] 24 25In POSIX-Extended regular expressions, all characters match themselves except for 26the following special characters: 27 28[pre .\[{}()\\\*+?|^$] 29 30[h4 Wildcard:] 31 32The single character '.' when used outside of a character set will match 33any single character except: 34 35* The NULL character when the flag `match_no_dot_null` is passed to the 36matching algorithms. 37* The newline character when the flag `match_not_dot_newline` is passed 38to the matching algorithms. 39 40[h4 Anchors:] 41 42A '^' character shall match the start of a line when used as the first 43character of an expression, or the first character of a sub-expression. 44 45A '$' character shall match the end of a line when used as the 46last character of an expression, or the last character of a sub-expression. 47 48[h4 Marked sub-expressions:] 49 50A section beginning `(` and ending `)` acts as a marked sub-expression. 51Whatever matched the sub-expression is split out in a separate field 52by the matching algorithms. Marked sub-expressions can also repeated, 53or referred to by a back-reference. 54 55[h4 Repeats:] 56 57Any atom (a single character, a marked sub-expression, or a character class) 58can be repeated with the `*`, `+`, `?`, and `{}` operators. 59 60The `*` operator will match the preceding atom /zero or more times/, for 61example the expression `a*b` will match any of the following: 62 63[pre 64b 65ab 66aaaaaaaab 67] 68 69The `+` operator will match the preceding atom /one or more times/, 70for example the expression a+b will match any of the following: 71 72[pre 73ab 74aaaaaaaab 75] 76 77But will not match: 78 79[pre 80b 81] 82 83The `?` operator will match the preceding atom /zero or one times/, for 84example the expression `ca?b` will match any of the following: 85 86[pre 87cb 88cab 89] 90But will not match: 91 92[pre 93caab 94] 95 96An atom can also be repeated with a bounded repeat: 97 98`a{n}` Matches 'a' repeated /exactly n times/. 99 100`a{n,}` Matches 'a' repeated /n or more times/. 101 102`a{n, m}` Matches 'a' repeated /between n and m times inclusive/. 103 104For example: 105 106[pre ^a{2,3}\$] 107 108Will match either of: 109 110 aa 111 aaa 112 113But neither of: 114 115 a 116 aaaa 117 118It is an error to use a repeat operator, if the preceding construct can not 119be repeated, for example: 120 121 a(*) 122 123Will raise an error, as there is nothing for the `*` operator to be applied to. 124 125[h4 Back references:] 126 127An escape character followed by a digit /n/, where /n/ is in the range 1-9, 128matches the same string that was matched by sub-expression /n/. For example 129the expression: 130 131[pre ^(a\*)\[\^a\]\*\\1\$] 132 133Will match the string: 134 135 aaabbaaa 136 137But not the string: 138 139 aaabba 140 141[caution The POSIX standard does not support back-references for "extended" 142regular expressions, this is a compatible extension to that standard.] 143 144[h4 Alternation] 145 146The `|` operator will match either of its arguments, so for example: 147`abc|def` will match either "abc" or "def". 148 149Parenthesis can be used to group alternations, for example: `ab(d|ef)` 150will match either of "abd" or "abef". 151 152[h4 Character sets:] 153 154A character set is a bracket-expression starting with [^\[] and ending with [^\]], 155it defines a set of characters, and matches any single character that is 156a member of that set. 157 158A bracket expression may contain any combination of the following: 159 160[h5 Single characters:] 161 162For example `[abc]`, will match any of the characters 'a', 'b', or 'c'. 163 164[h5 Character ranges:] 165 166For example `[a-c]` will match any single character in the range 'a' to 'c'. 167By default, for POSIX-Extended regular expressions, a character /x/ is 168within the range /y/ to /z/, if it collates within that range; this 169results in locale specific behavior . This behavior can be turned 170off by unsetting the `collate` 171[link boost_regex.ref.syntax_option_type option flag] - in which case whether 172a character appears within a range is determined by comparing the code 173points of the characters only. 174 175[h5 Negation:] 176 177If the bracket-expression begins with the ^ character, then it matches the 178complement of the characters it contains, for example `[^a-c]` matches 179any character that is not in the range `a-c`. 180 181[h5 Character classes:] 182 183An expression of the form `[[:name:]]` matches the named character class "name", 184for example `[[:lower:]]` matches any lower case character. 185See [link boost_regex.syntax.character_classes character class names]. 186 187[h5 Collating Elements:] 188 189An expression of the form `[[.col.]` matches the collating element /col/. 190A collating element is any single character, or any sequence of 191characters that collates as a single unit. Collating elements may 192also be used as the end point of a range, for example: `[[.ae.]-c]` 193matches the character sequence "ae", plus any single character 194in the range "ae"-c, assuming that "ae" is treated as a single 195collating element in the current locale. 196 197Collating elements may be used in place of escapes (which are not 198normally allowed inside character sets), for example `[[.^.]abc]` 199would match either one of the characters 'abc^'. 200 201As an extension, a collating element may also be specified via its 202[link boost_regex.syntax.collating_names symbolic name], for example: 203 204 [[.NUL.]] 205 206matches a NUL character. 207 208[h5 Equivalence classes:] 209 210An expression of the form `[[=col=]]`, matches any character or collating element 211whose primary sort key is the same as that for collating element /col/, 212as with collating elements the name /col/ may be a 213[link boost_regex.syntax.collating_names symbolic name]. A primary 214sort key is one that ignores case, accentation, or locale-specific tailorings; 215so for example `[[=a=]]` matches any of the characters: 216a, '''À''', '''Á''', '''Â''', 217'''Ã''', '''Ä''', '''Å''', A, '''à''', '''á''', 218'''â''', '''ã''', '''ä''' and '''å'''. 219Unfortunately implementation of this is reliant on the platform's 220collation and localisation support; this feature can not be relied 221upon to work portably across all platforms, or even all locales on one platform. 222 223[h5 Combinations:] 224 225All of the above can be combined in one character set declaration, 226for example: `[[:digit:]a-c[.NUL.]]`. 227 228[h4 Escapes] 229 230The POSIX standard defines no escape sequences for POSIX-Extended 231regular expressions, except that: 232 233* Any special character preceded by an escape shall match itself. 234* The effect of any ordinary character being preceded by an escape is undefined. 235* An escape inside a character class declaration shall match itself: in 236other words the escape character is not "special" inside a character 237class declaration; so `[\^]` will match either a literal '\\' or a '^'. 238 239However, that's rather restrictive, so the following standard-compatible 240extensions are also supported by Boost.Regex: 241 242[h5 Escapes matching a specific character] 243 244The following escape sequences are all synonyms for single characters: 245 246[table 247[[Escape][Character]] 248[[\\a]['\\a']] 249[[\\e][0x1B]] 250[[\\f][\\f]] 251[[\\n][\\n]] 252[[\\r][\\r]] 253[[\\t][\\t]] 254[[\\v][\\v]] 255[[\\b][\\b (but only inside a character class declaration).]] 256[[\\cX][An ASCII escape sequence - the character whose code point is X % 32]] 257[[\\xdd][A hexadecimal escape sequence - matches the single character whose code point is 0xdd.]] 258[[\\x{dddd}][A hexadecimal escape sequence - matches the single character whose code point is 0xdddd.]] 259[[\\0ddd][An octal escape sequence - matches the single character whose code point is 0ddd.]] 260[[\\N{Name}][Matches the single character which has the symbolic name ['Name]. For example `\\N{newline}` matches the single character \\n.]] 261] 262 263[h5 "Single character" character classes:] 264 265Any escaped character /x/, if /x/ is the name of a character class shall 266match any character that is a member of that class, and any 267escaped character /X/, if /x/ is the name of a character class, 268shall match any character not in that class. 269 270The following are supported by default: 271 272[table 273[[Escape sequence][Equivalent to]] 274[[`\d`][`[[:digit:]]`]] 275[[`\l`][`[[:lower:]]`]] 276[[`\s`][`[[:space:]]`]] 277[[`\u`][`[[:upper:]]`]] 278[[`\w`][`[[:word:]]`]] 279[[`\D`][`[^[:digit:]]`]] 280[[`\L`][`[^[:lower:]]`]] 281[[`\S`][`[^[:space:]]`]] 282[[`\U`][`[^[:upper:]]`]] 283[[`\W`][`[^[:word:]]`]] 284] 285 286[h5 Character Properties] 287 288The character property names in the following table are all equivalent to the 289names used in character classes. 290 291[table 292[[Form][Description][Equivalent character set form]] 293[[`\pX`][Matches any character that has the property X.][`[[:X:]]`]] 294[[`\p{Name}`][Matches any character that has the property Name.][`[[:Name:]]`]] 295[[`\PX`][Matches any character that does not have the property X.][`[^[:X:]]`]] 296[[`\P{Name}`][Matches any character that does not have the property Name.][`[^[:Name:]]`]] 297] 298 299For example `\pd` matches any "digit" character, as does `\p{digit}`. 300 301[h5 Word Boundaries] 302 303The following escape sequences match the boundaries of words: 304 305[table 306[[Escape][Meaning]] 307[[`\<`][Matches the start of a word.]] 308[[`\>`][Matches the end of a word.]] 309[[`\b`][Matches a word boundary (the start or end of a word).]] 310[[`\B`][Matches only when not at a word boundary.]] 311] 312 313[h5 Buffer boundaries] 314 315The following match only at buffer boundaries: a "buffer" in this 316context is the whole of the input text that is being matched against 317(note that ^ and $ may match embedded newlines within the text). 318 319[table 320[[Escape][Meaning]] 321[[\\\`][Matches at the start of a buffer only.]] 322[[\\'][Matches at the end of a buffer only.]] 323[[`\A`][Matches at the start of a buffer only (the same as \\\`).]] 324[[`\z`][Matches at the end of a buffer only (the same as \\').]] 325[[`\Z`][Matches an optional sequence of newlines at the end of a buffer: 326equivalent to the regular expression `\n*\z`]] 327] 328 329[h5 Continuation Escape] 330 331The sequence `\G` matches only at the end of the last match found, or at 332the start of the text being matched if no previous match was found. 333This escape useful if you're iterating over the matches contained within 334a text, and you want each subsequence match to start where the last one ended. 335 336[h5 Quoting escape] 337 338The escape sequence `\Q` begins a "quoted sequence": all the subsequent 339characters are treated as literals, until either the end of the 340regular expression or `\E` is found. For example the expression: `\Q\*+\Ea+` 341would match either of: 342 343 \*+a 344 \*+aaa 345 346[h5 Unicode escapes] 347 348[table 349[[Escape][Meaning]] 350[[`\C`][Matches a single code point: in Boost regex this has exactly the same effect as a "." operator.]] 351[[`\X`][Matches a combining character sequence: that is any non-combining character followed by a sequence of zero or more combining characters.]] 352] 353 354[h5 Any other escape] 355 356Any other escape sequence matches the character that is escaped, 357for example \\@ matches a literal '@'. 358 359[h4 Operator precedence] 360 361The order of precedence for of operators is as follows: 362 363# Collation-related bracket symbols `[==] [::] [..]` 364# Escaped characters `\` 365# Character set (bracket expression) `[]` 366# Grouping `()` 367# Single-character-ERE duplication `* + ? {m,n}` 368# Concatenation 369# Anchoring ^$ 370# Alternation `|` 371 372[h4 What Gets Matched] 373 374When there is more that one way to match a regular expression, the 375"best" possible match is obtained using the 376[link boost_regex.syntax.leftmost_longest_rule leftmost-longest rule]. 377 378[h3 Variations] 379 380[h4 Egrep] 381 382When an expression is compiled with the 383[link boost_regex.ref.syntax_option_type flag `egrep`] set, then the 384expression is treated as a newline separated list of 385[link boost_regex.posix_extended_syntax POSIX-Extended expressions], 386a match is found if any of the 387expressions in the list match, for example: 388 389 boost::regex e("abc\ndef", boost::regex::egrep); 390 391will match either of the POSIX-Basic expressions "abc" or "def". 392 393As its name suggests, this behavior is consistent with the Unix utility `egrep`, 394and with grep when used with the -E option. 395 396[h4 awk] 397 398In addition to the 399[link boost_regex.posix_extended_syntax POSIX-Extended features] the 400escape character is 401special inside a character class declaration. 402 403In addition, some escape sequences that are not defined as part of 404POSIX-Extended specification are required to be supported - however Boost.Regex 405supports these by default anyway. 406 407[h3 Options] 408 409There are a [link boost_regex.ref.syntax_option_type.syntax_option_type_extended variety of flags] 410that may be combined with the `extended` and `egrep` options when 411constructing the regular expression, in particular note that the 412[link boost_regex.ref.syntax_option_type.syntax_option_type_extended `newline_alt`] 413option alters the syntax, while the 414[link boost_regex.ref.syntax_option_type.syntax_option_type_extended `collate`, `nosubs` 415and `icase` options] modify how the case and locale sensitivity are to be applied. 416 417[h3 References] 418 419[@http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap09.html 420IEEE Std 1003.1-2001, Portable Operating System Interface (POSIX ), Base Definitions and Headers, Section 9, Regular Expressions.] 421 422[@http://www.opengroup.org/onlinepubs/000095399/utilities/grep.html 423IEEE Std 1003.1-2001, Portable Operating System Interface (POSIX ), Shells and Utilities, Section 4, Utilities, egrep.] 424 425[@http://www.opengroup.org/onlinepubs/000095399/utilities/awk.html 426IEEE Std 1003.1-2001, Portable Operating System Interface (POSIX ), Shells and Utilities, Section 4, Utilities, awk.] 427 428[endsect] 429 430 431