1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Boost.Regex: POSIX-Extended Regular Expression Syntax</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK href="../../../boost.css" type="text/css" rel="stylesheet"></head>
<body>
<P>
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="0">
<TR>
<td vAlign="top" width="300">
<h3><A href="../../../index.htm"><IMG height="86" alt="C++ Boost" src="../../../boost.png" width="277" border="0"></A></h3>
</td>
<TD width="353">
<H1 align="center">Boost.Regex</H1>
<H2 align="center">POSIX-Extended Regular Expression Syntax</H2>
</TD>
<td width="50">
<h3><A href="index.html"><IMG height="45" alt="Boost.Regex Index" src="uarrow.gif" width="43" border="0"></A></h3>
</td>
</TR>
</TABLE>
</P>
<HR>
<H3>Contents</H3>
<dl class="index">
<dt><A href="#synopsis">Synopsis</A> <dt><A href="#extended">POSIX Extended Syntax</A>
<dt><A href="#variations">Variations</A>
<dd>
<dl>
<dt><A href="#egrep">egrep</A> <dt><A href="#awk">awk</A> </dt>
</dl>
<dt><A href="#options">Options</A> <dt><A href="#refs">References</A></dt>
</dl>
<H3><A name="synopsis"></A>Synopsis</H3>
<P>The POSIX-Extended regular expression syntax is supported by the POSIX C
regular expression API's, and variations are used by the utilities <EM>egrep</EM>
and <EM>awk</EM>. You can construct POSIX extended regular expressions in
Boost.Regex by passing the flag <EM>extended</EM> to the regex constructor, for
example:</P>
<PRE>// e1 is a case sensitive POSIX-Extended expression:
boost::regex e1(my_expression, boost::regex::extended);
// e2 a case insensitive POSIX-Extended expression:
boost::regex e2(my_expression, boost::regex::extended|boost::regex::icase);</PRE>
<H3>POSIX Extended Syntax<A name="extended"></A></H3>
<P>In POSIX-Extended regular expressions, all characters match themselves except
for the following special characters:</P>
<PRE>.[{()\*+?|^$</PRE>
<H4>Wildcard:</H4>
<P>The single character '.' when used outside of a character set will match any
single character except:</P>
<P>The NULL character when the flag <EM>match_no_dot_null</EM> is passed to the
matching algorithms.</P>
<P>The newline character when the flag <EM>match_not_dot_newline</EM> is passed to
the matching algorithms.</P>
<H4>Anchors:</H4>
<P>A '^' character shall match the start of a line when used as the first
character of an expression, or the first character of a sub-expression.</P>
<P>A '$' character shall match the end of a line when used as the last character
of an expression, or the last character of a sub-expression.</P>
<H4>Marked sub-expressions:</H4>
<P>A section beginning ( and ending ) acts as a marked sub-expression.
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.</P>
<H4>Repeats:</H4>
<P>Any atom (a single character, a marked sub-expression, or a character class)
can be repeated with the *, +, ?, and {} operators.</P>
<P>The * operator will match the preceding atom zero or more times, for example
the expression a*b will match any of the following:</P>
<PRE>b
ab
aaaaaaaab</PRE>
<P>The + operator will match the preceding atom one or more times, for example the
expression a+b will match any of the following:</P>
<PRE>ab
aaaaaaaab</PRE>
<P>But will not match:</P>
<PRE>b</PRE>
<P>The ? operator will match the preceding atom zero or one times, for
example the expression ca?b will match any of the following:</P>
<PRE>cb
cab</PRE>
<P>But will not match:</P>
<PRE>caab</PRE>
<P>An atom can also be repeated with a bounded repeat:</P>
<P>a{n} Matches 'a' repeated exactly <EM>n</EM> times.</P>
<P>a{n,} Matches 'a' repeated <EM>n</EM> or more times.</P>
<P>a{n, m} Matches 'a' repeated between <EM>n</EM> and <EM>m</EM> times
inclusive.</P>
<P>For example:</P>
<PRE>^a{2,3}$</PRE>
<P>Will match either of:</P>
<PRE>aa
aaa</PRE>
<P>But neither of:</P>
<PRE>a
aaaa</PRE>
<P>It is an error to use a repeat operator, if the preceding construct can not be
repeated, for example:</P>
<PRE>a(*)</PRE>
<P>Will raise an error, as there is nothing for the * operator to be applied to.</P>
<H4>Back references:</H4>
<P>An escape character followed by a digit <EM>n</EM>, where <EM>n </EM>is in the
range 1-9, matches the same string that was matched by sub-expression <EM>n</EM>.
For example the expression:</P>
<PRE>^(a*).*\1$</PRE>
<P>Will match the string:</P>
<PRE>aaabbaaa</PRE>
<P>But not the string:</P>
<PRE>aaabba</PRE>
<P><EM><STRONG>Caution</STRONG>: the POSIX standard does not support back-references
for "extended" regular expressions, this is a compatible extension to that
standard.</EM></P>
<H4>Alternation</H4>
<P>The | operator will match either of its arguments, so for example: abc|def will
match either "abc" or "def".
</P>
<P>Parenthesis can be used to group alternations, for example: ab(d|ef) will match
either of "abd" or "abef".</P>
<H4>Character sets:</H4>
<P>A character set is a bracket-expression starting with [ and ending with ], it
defines a set of characters, and matches any single character that is a member
of that set.</P>
<P>A bracket expression may contain any combination of the following:</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<H5>Single characters:</H5>
<P>For example [abc], will match any of the characters 'a', 'b', or 'c'.</P>
<H5>Character ranges:</H5>
<P>For example [a-c] will match any single character in the range 'a' to
'c'. By default, for POSIX-Extended regular expressions, a character <EM>x</EM>
is within the range <EM>y</EM> to <EM>z</EM>, if it collates within that
range; <EM><STRONG>this results in locale specific behavior</STRONG></EM> .
This behavior can be turned off by unsetting the <EM><A href="syntax_option_type.html#extended">
collate</A></EM> option flag - in which case whether a character appears
within a range is determined by comparing the code points of the characters
only.</P>
<H5>Negation:</H5>
<P>If the bracket-expression begins with the ^ character, then it matches the
complement of the characters it contains, for example [^a-c] matches any
character that is not in the range a-c.</P>
<H5>Character classes:</H5>
<P>An expression of the form [[:name:]] matches the named character class "name",
for example [[:lower:]] matches any lower case character. See <A href="character_class_names.html">
character class names</A>.</P>
<H5>Collating Elements:</H5>
<P>An expression of the form [[.col.] matches the collating element <EM>col</EM>.
A collating element is any single character, or any sequence of characters that
collates as a single unit. Collating elements may also be used as the end
point of a range, for example: [[.ae.]-c] matches the 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.</P>
<P>Collating elements may be used in place of escapes (which are not normally
allowed inside character sets), for example [[.^.]abc] would match either one
of the characters 'abc^'.</P>
<P>As an extension, a collating element may also be specified via its <A href="collating_names.html">
symbolic name</A>, for example:</P>
<P>[[.NUL.]]</P>
<P>matches a NUL character.</P>
<H5>Equivalence classes:</H5>
<P>
An expression oftheform[[=col=]], matches any character or collating element
whose primary sort key is the same as that for collating element <EM>col</EM>,
as with colating elements the name <EM>col</EM> may be a <A href="collating_names.html">
symbolic name</A>. 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.</P>
</BLOCKQUOTE>
<H5>Combinations:</H5>
<P>All of the above can be combined in one character set declaration, for example:
[[:digit:]a-c[.NUL.]].</P>
<H4>Escapes</H4>
<P>The POSIX standard defines no escape sequences for POSIX-Extended regular
expressions, except that:</P>
<UL>
<LI>
Any special character preceded by an escape shall match itself.
<LI>
The effect of any ordinary character being preceded by an escape is undefined.
<LI>
An escape inside a character class declaration shall match itself: in other
words the escape character is not "special" inside a character class
declaration; so [\^] will match either a literal '\' or a '^'.</LI></UL>
<P>However, that's rather restrictive, so the following standard-compatible
extensions are also supported by Boost.Regex:</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<H5>Escapes matching a specific character</H5>
<P>The following escape sequences are all synonyms for single characters:</P>
<P>
<TABLE id="Table7" cellSpacing="1" cellPadding="1" width="100%" border="1">
<TR>
<TD><STRONG>Escape</STRONG></TD>
<TD><STRONG>Character</STRONG></TD>
</TR>
<TR>
<TD>\a</TD>
<TD>'\a'</TD>
</TR>
<TR>
<TD>\e</TD>
<TD>0x1B</TD>
</TR>
<TR>
<TD>\f</TD>
<TD>\f</TD>
</TR>
<TR>
<TD>\n</TD>
<TD>\n</TD>
</TR>
<TR>
<TD>\r</TD>
<TD>\r</TD>
</TR>
<TR>
<TD>\t</TD>
<TD>\t</TD>
</TR>
<TR>
<TD>\v</TD>
<TD>\v</TD>
</TR>
<TR>
<TD>\b</TD>
<TD>\b (but only inside a character class declaration).</TD>
</TR>
<TR>
<TD>\cX</TD>
<TD>An ASCII escape sequence - the character whose code point is X % 32</TD>
</TR>
<TR>
<TD>\xdd</TD>
<TD>A hexadecimal escape sequence - matches the single character whose code point
is 0xdd.</TD>
</TR>
<TR>
<TD>\x{dddd}</TD>
<TD>A hexadecimal escape sequence - matches the single character whose code point
is 0xdddd.</TD>
</TR>
<TR>
<TD>\0ddd</TD>
<TD>An octal escape sequence - matches the single character whose code point is
0ddd.</TD>
</TR>
<TR>
<TD>\N{Name}</TD>
<TD>Matches the single character which has the <A href="collating_names.html">symbolic
name</A> <EM>name. </EM>For example \N{newline} matches the single
character \n.</TD>
</TR>
</TABLE>
</P>
<H5>"Single character" character classes:</H5>
<P>Any escaped character <EM>x</EM>, if <EM>x</EM> is the name of a character
class shall match any character that is a member of that class, and any escaped
character <EM>X</EM>, if <EM>x</EM> is the name of a character class, shall
match any character not in that class.</P>
<P>The following are supported by default:</P>
<P>
<TABLE id="Table3" cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD><STRONG>Escape sequence</STRONG></TD>
<TD><STRONG>Equivalent to</STRONG></TD>
</TR>
<TR>
<TD>\d</TD>
<TD>[[:digit:]]</TD>
</TR>
<TR>
<TD>\l</TD>
<TD>[[:lower:]]</TD>
</TR>
<TR>
<TD>\s</TD>
<TD>[[:space:]]</TD>
</TR>
<TR>
<TD>\u</TD>
<TD>[[:upper:]]</TD>
</TR>
<TR>
<TD>\w</TD>
<TD>[[:word:]]</TD>
</TR>
<TR>
<TD>\D</TD>
<TD>[^[:digit:]]</TD>
</TR>
<TR>
<TD>\L</TD>
<TD>[^[:lower:]]</TD>
</TR>
<TR>
<TD>\S</TD>
<TD>[^[:space:]]</TD>
</TR>
<TR>
<TD>\U</TD>
<TD>[^[:upper:]]</TD>
</TR>
<TR>
<TD>\W</TD>
<TD>[^[:word:]]</TD>
</TR>
</TABLE>
</P>
<H5>
<H5>Character Properties</H5>
</H5>
<P dir="ltr">The character property names in the following table are all
equivalent to the <A href="character_class_names.html">names used in character
classes</A>.</P>
<H5>
<TABLE id="Table9" cellSpacing="1" cellPadding="1" width="100%" border="0">
<TR>
<TD><STRONG>Form</STRONG></TD>
<TD><STRONG>Description</STRONG></TD>
<TD><STRONG>Equivalent character set form</STRONG></TD>
</TR>
<TR>
<TD>\pX</TD>
<TD>Matches any character that has the property X.</TD>
<TD>[[:X:]]</TD>
</TR>
<TR>
<TD>\p{Name}</TD>
<TD>Matches any character that has the property <EM>Name</EM>.</TD>
<TD>[[:Name:]]</TD>
</TR>
<TR>
<TD>\PX</TD>
<TD>Matches any character that does not have the property X.</TD>
<TD>[^[:X:]]</TD>
</TR>
<TR>
<TD>\P{Name}</TD>
<TD>Matches any character that does not have the property <EM>Name</EM>.</TD>
<TD>[^[:Name:]]</TD>
</TR>
</TABLE>
</H5>
<H5>Word Boundaries</H5>
<P>The following escape sequences match the boundaries of words:</P>
<P>
<TABLE id="Table4" cellSpacing="1" cellPadding="1" width="100%" border="1">
<TR>
<TD>\<</TD>
<TD>Matches the start of a word.</TD>
</TR>
<TR>
<TD>\></TD>
<TD>Matches the end of a word.</TD>
</TR>
<TR>
<TD>\b</TD>
<TD>Matches a word boundary (the start or end of a word).</TD>
</TR>
<TR>
<TD>\B</TD>
<TD>Matches only when not at a word boundary.</TD>
</TR>
</TABLE>
</P>
<H5>Buffer boundaries</H5>
<P>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).</P>
<P>
<TABLE id="Table5" cellSpacing="1" cellPadding="1" width="100%" border="1">
<TR>
<TD>\`</TD>
<TD>Matches at the start of a buffer only.</TD>
</TR>
<TR>
<TD>\'</TD>
<TD>Matches at the end of a buffer only.</TD>
</TR>
<TR>
<TD>\A</TD>
<TD>Matches at the start of a buffer only (the same as \`).</TD>
</TR>
<TR>
<TD>\z</TD>
<TD>Matches at the end of a buffer only (the same as \').</TD>
</TR>
<TR>
<TD>\Z</TD>
<TD>Matches an optional sequence of newlines at the end of a buffer: equivalent to
the regular expression \n*\z</TD>
</TR>
</TABLE>
</P>
<H5>Continuation Escape</H5>
<P>The sequence \G matches only at the end of the last match found, or at 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.</P>
<H5>Quoting escape</H5>
<P>The escape sequence \Q begins a "quoted sequence": all the subsequent
characters are treated as literals, until either the end of the regular
expression or \E is found. For example the expression: \Q\*+\Ea+ would
match either of:</P>
<PRE>\*+a<BR>\*+aaa</PRE>
<H5>Unicode escapes</H5>
<P>
<TABLE id="Table6" cellSpacing="1" cellPadding="1" width="100%" border="1">
<TR>
<TD>\C</TD>
<TD>Matches a single code point: in Boost regex this has exactly the same effect
as a "." operator.</TD>
</TR>
<TR>
<TD>\X</TD>
<TD>Matches a combining character sequence: that is any non-combining character
followed by a sequence of zero or more combining characters.</TD>
</TR>
</TABLE>
</P>
<H5>Any other escape</H5>
<P>Any other escape sequence matches the character that is escaped, for example \@
matches a literal <A href="mailto:'@'">'@'</A>.</P>
</BLOCKQUOTE><A name="variations">
<H4>Operator precedence</H4>
<P> The order of precedence for of operators is as shown in the following
table:</P>
<P>
<TABLE id="Table2" cellSpacing="1" cellPadding="1" width="100%" border="1">
<TR>
<TD>Collation-related bracket symbols</TD>
<TD>[==] [::] [..]</TD>
</TR>
<TR>
<TD>Escaped characters
</TD>
<TD>\</TD>
</TR>
<TR>
<TD>Character set (bracket expression)
</TD>
<TD>[]</TD>
</TR>
<TR>
<TD>Grouping</TD>
<TD>()</TD>
</TR>
<TR>
<TD>Single-character-ERE duplication
</TD>
<TD>* + ? {m,n}</TD>
</TR>
<TR>
<TD>Concatenation</TD>
<TD></TD>
</TR>
<TR>
<TD>Anchoring</TD>
<TD>^$</TD>
</TR>
<TR>
<TD>Alternation</TD>
<TD>|</TD>
</TR>
</TABLE>
</P>
</A>
<H4>What Gets Matched</H4>
<P>When there is more that one way to match a regular expression, the "best"
possible match is obtained using the <A href="syntax_leftmost_longest.html">leftmost-longest
rule</A>.</P>
<H3>Variations</H3>
<H4>Egrep<A name="egrep"></H4>
<P>When an expression is compiled with the flag <EM>egrep</EM> set, then the
expression is treated as a newline separated list of POSIX-Extended
expressions, a match is found if any of the expressions in the list match, for
example:</P>
<PRE>boost::regex e("abc\ndef", boost::regex::egrep);</PRE>
<P>will match either of the POSIX-Basic expressions "abc" or "def".</P>
<P>As its name suggests, this behavior is consistent with the Unix utility <EM>egrep</EM>,
and with <EM>grep</EM> when used with the -E option.</P>
<H4>awk<A name="awk"></A></H4>
<P>In addition to the <A href="#extended">POSIX-Extended features</A> the
escape character is special inside a character class declaration. </P>
<P>In addition, some escape sequences that are not defined as part of
POSIX-Extended specification are required to be supported - however Boost.Regex
supports these by default anyway.</P>
<H3><A name="options"></A>Options</H3>
<P>There are a <A href="syntax_option_type.html#extended">variety of flags</A> that
may be combined with the <EM>extended</EM> and <EM>egrep</EM> options when
constructing the regular expression, in particular note that the <A href="syntax_option_type.html#extended">
newline_alt</A> option alters the syntax, while the <A href="syntax_option_type.html#extended">
collate, nosubs and icase</A> options modify how the case and locale
sensitivity are to be applied.</P>
<H3><A name="refs">References</H3>
<P><A href="http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap09.html"> IEEE
Std 1003.1-2001, Portable Operating System Interface (POSIX ), Base Definitions
and Headers, Section 9, Regular Expressions.</A></P>
<P><A href="http://www.opengroup.org/onlinepubs/000095399/utilities/grep.html"> IEEE
Std 1003.1-2001, Portable Operating System Interface (POSIX ), Shells and
Utilities, Section 4, Utilities, egrep.</A></P>
<P><A href="http://www.opengroup.org/onlinepubs/000095399/utilities/awk.html">IEEE
Std 1003.1-2001, Portable Operating System Interface (POSIX ), Shells and
Utilities, Section 4, Utilities, awk.</A></P>
<HR>
<P></P>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
21 Aug 2004
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
<P><I> Copyright <a href="mailto:jm@regex.fsnet.co.uk">John Maddock</a> 2004</I></P>
<I>
<P><I>Use, modification and distribution are subject to the Boost Software License,
Version 1.0. (See accompanying file <A href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A>
or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>).</I></P>
</I>
</body>
</html>
|