syntax_extended.qbk 14 KB

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