| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|---|
| 2 | <html> |
|---|
| 3 | <head> |
|---|
| 4 | <title>Boost.Regex: Perl Regular Expression Syntax</title> |
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
|---|
| 6 | <LINK href="../../../boost.css" type="text/css" rel="stylesheet"></head> |
|---|
| 7 | <body> |
|---|
| 8 | <P> |
|---|
| 9 | <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="0"> |
|---|
| 10 | <TR> |
|---|
| 11 | <td vAlign="top" width="300"> |
|---|
| 12 | <h3><A href="../../../index.htm"><IMG height="86" alt="C++ Boost" src="../../../boost.png" width="277" border="0"></A></h3> |
|---|
| 13 | </td> |
|---|
| 14 | <TD width="353"> |
|---|
| 15 | <H1 align="center">Boost.Regex</H1> |
|---|
| 16 | <H2 align="center"> |
|---|
| 17 | Perl Regular Expression Syntax</H2> |
|---|
| 18 | </TD> |
|---|
| 19 | <td width="50"> |
|---|
| 20 | <h3><A href="index.html"><IMG height="45" alt="Boost.Regex Index" src="uarrow.gif" width="43" border="0"></A></h3> |
|---|
| 21 | </td> |
|---|
| 22 | </TR> |
|---|
| 23 | </TABLE> |
|---|
| 24 | </P> |
|---|
| 25 | <HR> |
|---|
| 26 | <H3>Contents</H3> |
|---|
| 27 | <dl class="index"> |
|---|
| 28 | <dt><A href="#synopsis">Synopsis</A> <dt><A href="#Perl">Perl Syntax</A> <dt><A href="#what"> |
|---|
| 29 | What Gets Matched</A> <dt><A href="#variations">Variations</A> |
|---|
| 30 | <dd> |
|---|
| 31 | <dt><A href="#options">Options</A> <dt><A href="#mods">Modifiers</A> <dt><A href="#refs">References</A></dt> |
|---|
| 32 | </dl> |
|---|
| 33 | <H3><A name="synopsis"></A>Synopsis</H3> |
|---|
| 34 | <P>The Perl regular expression syntax is based on that used by the programming |
|---|
| 35 | language <EM>Perl</EM> . Perl regular expressions are the default |
|---|
| 36 | behavior in Boost.Regex or you can pass the flag <EM>perl</EM> to the |
|---|
| 37 | regex constructor, for example:</P> |
|---|
| 38 | <PRE>// e1 is a case sensitive Perl regular expression: |
|---|
| 39 | // since Perl is the default option there's no need to explicitly specify the syntax used here: |
|---|
| 40 | boost::regex e1(my_expression); |
|---|
| 41 | // e2 a case insensitive Perl regular expression: |
|---|
| 42 | boost::regex e2(my_expression, boost::regex::perl|boost::regex::icase);</PRE> |
|---|
| 43 | <H3>Perl Regular Expression Syntax<A name="Perl"></A></H3> |
|---|
| 44 | <P>In Perl regular expressions, all characters match themselves except for |
|---|
| 45 | the following special characters:</P> |
|---|
| 46 | <PRE>.[{()\*+?|^$</PRE> |
|---|
| 47 | <H4>Wildcard:</H4> |
|---|
| 48 | <P>The single character '.' when used outside of a character set will match any |
|---|
| 49 | single character except:</P> |
|---|
| 50 | <P>The NULL character when the flag <EM>match_no_dot_null</EM> is passed to the |
|---|
| 51 | matching algorithms.</P> |
|---|
| 52 | <P>The newline character when the flag <EM>match_not_dot_newline</EM> is passed to |
|---|
| 53 | the matching algorithms.</P> |
|---|
| 54 | <H4>Anchors:</H4> |
|---|
| 55 | <P>A '^' character shall match the start of a line.</P> |
|---|
| 56 | <P>A '$' character shall match the end of a line.</P> |
|---|
| 57 | <H4>Marked sub-expressions:</H4> |
|---|
| 58 | <P>A section beginning ( and ending ) acts as a marked sub-expression. |
|---|
| 59 | Whatever matched the sub-expression is split out in a separate field by the |
|---|
| 60 | matching algorithms. Marked sub-expressions can also repeated, or |
|---|
| 61 | referred to by a back-reference.</P> |
|---|
| 62 | <H4>Non-marking grouping:</H4> |
|---|
| 63 | <P>A marked sub-expression is useful to lexically group part of a regular |
|---|
| 64 | expression, but has the side-effect of spitting out an extra field in the |
|---|
| 65 | result. As an alternative you can lexically group part of a regular |
|---|
| 66 | expression, without generating a marked sub-expression by using (?: and ) , for |
|---|
| 67 | example (?:ab)+ will repeat "ab" without splitting out any separate |
|---|
| 68 | sub-expressions.</P> |
|---|
| 69 | <H4>Repeats:</H4> |
|---|
| 70 | <P>Any atom (a single character, a marked sub-expression, or a character class) |
|---|
| 71 | can be repeated with the *, +, ?, and {} operators.</P> |
|---|
| 72 | <P>The * operator will match the preceding atom zero or more times, for example |
|---|
| 73 | the expression a*b will match any of the following:</P> |
|---|
| 74 | <PRE>b |
|---|
| 75 | ab |
|---|
| 76 | aaaaaaaab</PRE> |
|---|
| 77 | <P>The + operator will match the preceding atom one or more times, for example the |
|---|
| 78 | expression a+b will match any of the following:</P> |
|---|
| 79 | <PRE>ab |
|---|
| 80 | aaaaaaaab</PRE> |
|---|
| 81 | <P>But will not match:</P> |
|---|
| 82 | <PRE>b</PRE> |
|---|
| 83 | <P>The ? operator will match the preceding atom zero or one times, for |
|---|
| 84 | example the expression ca?b will match any of the following:</P> |
|---|
| 85 | <PRE>cb |
|---|
| 86 | cab</PRE> |
|---|
| 87 | <P>But will not match:</P> |
|---|
| 88 | <PRE>caab</PRE> |
|---|
| 89 | <P>An atom can also be repeated with a bounded repeat:</P> |
|---|
| 90 | <P>a{n} Matches 'a' repeated exactly <EM>n</EM> times.</P> |
|---|
| 91 | <P>a{n,} Matches 'a' repeated <EM>n</EM> or more times.</P> |
|---|
| 92 | <P>a{n, m} Matches 'a' repeated between <EM>n</EM> and <EM>m</EM> times |
|---|
| 93 | inclusive.</P> |
|---|
| 94 | <P>For example:</P> |
|---|
| 95 | <PRE>^a{2,3}$</PRE> |
|---|
| 96 | <P>Will match either of:</P> |
|---|
| 97 | <PRE>aa |
|---|
| 98 | aaa</PRE> |
|---|
| 99 | <P>But neither of:</P> |
|---|
| 100 | <PRE>a |
|---|
| 101 | aaaa</PRE> |
|---|
| 102 | <P>It is an error to use a repeat operator, if the preceding construct can not be |
|---|
| 103 | repeated, for example:</P> |
|---|
| 104 | <PRE>a(*)</PRE> |
|---|
| 105 | <P>Will raise an error, as there is nothing for the * operator to be applied to.</P> |
|---|
| 106 | <H4>Non greedy repeats</H4> |
|---|
| 107 | <P>The normal repeat operators are "greedy", that is to say they will consume as |
|---|
| 108 | much input as possible. There are non-greedy versions available that will |
|---|
| 109 | consume as little input as possible while still producing a match.</P> |
|---|
| 110 | <P>*? Matches the previous atom zero or more times, while consuming as little |
|---|
| 111 | input as possible.</P> |
|---|
| 112 | <P>+? Matches the previous atom one or more times, while consuming as little input |
|---|
| 113 | as possible.</P> |
|---|
| 114 | <P>?? Matches the previous atom zero or one times, while consuming as little input |
|---|
| 115 | as possible.</P> |
|---|
| 116 | <P>{n,}? Matches the previous atom <EM>n</EM> or more times, while consuming |
|---|
| 117 | as little input as possible.</P> |
|---|
| 118 | <P>{n,m}? Matches the previous atom between <EM>n</EM> and <EM>m</EM> times, |
|---|
| 119 | while consuming as little input as possible.</P> |
|---|
| 120 | <H4>Back references:</H4> |
|---|
| 121 | <P>An escape character followed by a digit <EM>n</EM>, where <EM>n </EM>is in the |
|---|
| 122 | range 1-9, matches the same string that was matched by sub-expression <EM>n</EM>. |
|---|
| 123 | For example the expression:</P> |
|---|
| 124 | <PRE>^(a*).*\1$</PRE> |
|---|
| 125 | <P>Will match the string:</P> |
|---|
| 126 | <PRE>aaabbaaa</PRE> |
|---|
| 127 | <P>But not the string:</P> |
|---|
| 128 | <PRE>aaabba</PRE> |
|---|
| 129 | <H4>Alternation</H4> |
|---|
| 130 | <P>The | operator will match either of its arguments, so for example: abc|def will |
|---|
| 131 | match either "abc" or "def". |
|---|
| 132 | </P> |
|---|
| 133 | <P>Parenthesis can be used to group alternations, for example: ab(d|ef) will match |
|---|
| 134 | either of "abd" or "abef".</P> |
|---|
| 135 | <P>Empty alternatives are not allowed (these are almost always a mistake), |
|---|
| 136 | but if you really want an empty alternative use (?:) as a placeholder, for |
|---|
| 137 | example:</P> |
|---|
| 138 | <BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px"> |
|---|
| 139 | <P>"|abc" is not a valid expression, but<BR> |
|---|
| 140 | "(?:)|abc" is and is equivalent, also the expression:<BR> |
|---|
| 141 | "(?:abc)??" has exactly the same effect.</P> |
|---|
| 142 | </BLOCKQUOTE> |
|---|
| 143 | <H4>Character sets:</H4> |
|---|
| 144 | <P>A character set is a bracket-expression starting with [ and ending with ], it |
|---|
| 145 | defines a set of characters, and matches any single character that is a member |
|---|
| 146 | of that set.</P> |
|---|
| 147 | <P>A bracket expression may contain any combination of the following:</P> |
|---|
| 148 | <BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px"> |
|---|
| 149 | <H5>Single characters:</H5> |
|---|
| 150 | <P>For example [abc], will match any of the characters 'a', 'b', or 'c'.</P> |
|---|
| 151 | <H5>Character ranges:</H5> |
|---|
| 152 | <P>For example [a-c] will match any single character in the range 'a' to |
|---|
| 153 | 'c'. By default, for POSIX-Perl regular expressions, a character <EM>x</EM> |
|---|
| 154 | is within the range <EM>y</EM> to <EM>z</EM>, if it collates within that |
|---|
| 155 | range; this results in locale specific behavior. This behavior can |
|---|
| 156 | be turned off by unsetting the <EM><A href="syntax_option_type.html#Perl">collate</A></EM> |
|---|
| 157 | option flag - in which case whether a character appears within a range is |
|---|
| 158 | determined by comparing the code points of the characters only</P> |
|---|
| 159 | <H5>Negation:</H5> |
|---|
| 160 | <P>If the bracket-expression begins with the ^ character, then it matches the |
|---|
| 161 | complement of the characters it contains, for example [^a-c] matches any |
|---|
| 162 | character that is not in the range a-c.</P> |
|---|
| 163 | <H5>Character classes:</H5> |
|---|
| 164 | <P>An expression of the form [[:name:]] matches the named character class "name", |
|---|
| 165 | for example [[:lower:]] matches any lower case character. See <A href="character_class_names.html"> |
|---|
| 166 | character class names</A>.</P> |
|---|
| 167 | <H5>Collating Elements:</H5> |
|---|
| 168 | <P>An expression of the form [[.col.] matches the collating element <EM>col</EM>. |
|---|
| 169 | A collating element is any single character, or any sequence of characters that |
|---|
| 170 | collates as a single unit. Collating elements may also be used as the end |
|---|
| 171 | point of a range, for example: [[.ae.]-c] matches the character sequence "ae", |
|---|
| 172 | plus any single character in the range "ae"-c, assuming that "ae" is treated as |
|---|
| 173 | a single collating element in the current locale.</P> |
|---|
| 174 | <P>As an extension, a collating element may also be specified via it's <A href="collating_names.html"> |
|---|
| 175 | symbolic name</A>, for example:</P> |
|---|
| 176 | <P>[[.NUL.]]</P> |
|---|
| 177 | <P>matches a NUL character.</P> |
|---|
| 178 | <H5>Equivalence classes:</H5> |
|---|
| 179 | <P> |
|---|
| 180 | An expression oftheform[[=col=]], matches any character or collating element |
|---|
| 181 | whose primary sort key is the same as that for collating element <EM>col</EM>, |
|---|
| 182 | as with colating elements the name <EM>col</EM> may be a <A href="collating_names.html"> |
|---|
| 183 | symbolic name</A>. A primary sort key is one that ignores case, |
|---|
| 184 | accentation, or locale-specific tailorings; so for example [[=a=]] matches any |
|---|
| 185 | of the characters: a, à, á, â, ã, ä, å, A, À, Á, Â, Ã, Ä and Å. |
|---|
| 186 | Unfortunately implementation of this is reliant on the platform's collation and |
|---|
| 187 | localisation support; this feature can not be relied upon to work portably |
|---|
| 188 | across all platforms, or even all locales on one platform.</P> |
|---|
| 189 | <H5>Escapes:</H5> |
|---|
| 190 | <P>All the escape sequences that match a single character, or a single character |
|---|
| 191 | class are permitted within a character class definition, <EM>except</EM> the |
|---|
| 192 | negated character classes (\D \W etc).</P> |
|---|
| 193 | </BLOCKQUOTE> |
|---|
| 194 | <H5>Combinations:</H5> |
|---|
| 195 | <P>All of the above can be combined in one character set declaration, for example: |
|---|
| 196 | [[:digit:]a-c[.NUL.]].</P> |
|---|
| 197 | <H4>Escapes</H4> |
|---|
| 198 | <P>Any special character preceded by an escape shall match itself. |
|---|
| 199 | </P> |
|---|
| 200 | <P>The following escape sequences are also supported:</P> |
|---|
| 201 | <BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px"> |
|---|
| 202 | <H5>Escapes matching a specific character</H5> |
|---|
| 203 | <P>The following escape sequences are all synonyms for single characters:</P> |
|---|
| 204 | <P> |
|---|
| 205 | <TABLE id="Table7" cellSpacing="1" cellPadding="1" width="100%" border="1"> |
|---|
| 206 | <TR> |
|---|
| 207 | <TD><STRONG>Escape</STRONG></TD> |
|---|
| 208 | <TD><STRONG>Character</STRONG></TD> |
|---|
| 209 | </TR> |
|---|
| 210 | <TR> |
|---|
| 211 | <TD>\a</TD> |
|---|
| 212 | <TD>'\a'</TD> |
|---|
| 213 | </TR> |
|---|
| 214 | <TR> |
|---|
| 215 | <TD>\e</TD> |
|---|
| 216 | <TD>0x1B</TD> |
|---|
| 217 | </TR> |
|---|
| 218 | <TR> |
|---|
| 219 | <TD>\f</TD> |
|---|
| 220 | <TD>\f</TD> |
|---|
| 221 | </TR> |
|---|
| 222 | <TR> |
|---|
| 223 | <TD>\n</TD> |
|---|
| 224 | <TD>\n</TD> |
|---|
| 225 | </TR> |
|---|
| 226 | <TR> |
|---|
| 227 | <TD>\r</TD> |
|---|
| 228 | <TD>\r</TD> |
|---|
| 229 | </TR> |
|---|
| 230 | <TR> |
|---|
| 231 | <TD>\t</TD> |
|---|
| 232 | <TD>\t</TD> |
|---|
| 233 | </TR> |
|---|
| 234 | <TR> |
|---|
| 235 | <TD>\v</TD> |
|---|
| 236 | <TD>\v</TD> |
|---|
| 237 | </TR> |
|---|
| 238 | <TR> |
|---|
| 239 | <TD>\b</TD> |
|---|
| 240 | <TD>\b (but only inside a character class declaration).</TD> |
|---|
| 241 | </TR> |
|---|
| 242 | <TR> |
|---|
| 243 | <TD>\cX</TD> |
|---|
| 244 | <TD>An ASCII escape sequence - the character whose code point is X % 32</TD> |
|---|
| 245 | </TR> |
|---|
| 246 | <TR> |
|---|
| 247 | <TD>\xdd</TD> |
|---|
| 248 | <TD>A hexadecimal escape sequence - matches the single character whose code point |
|---|
| 249 | is 0xdd.</TD> |
|---|
| 250 | </TR> |
|---|
| 251 | <TR> |
|---|
| 252 | <TD>\x{dddd}</TD> |
|---|
| 253 | <TD>A hexadecimal escape sequence - matches the single character whose code point |
|---|
| 254 | is 0xdddd.</TD> |
|---|
| 255 | </TR> |
|---|
| 256 | <TR> |
|---|
| 257 | <TD>\0ddd</TD> |
|---|
| 258 | <TD>An octal escape sequence - matches the single character whose code point is |
|---|
| 259 | 0ddd.</TD> |
|---|
| 260 | </TR> |
|---|
| 261 | <TR> |
|---|
| 262 | <TD>\N{name}</TD> |
|---|
| 263 | <TD>Matches the single character which has the <A href="collating_names.html">symbolic |
|---|
| 264 | name</A> <EM>name. </EM>For example \N{newline} matches the single |
|---|
| 265 | character \n.</TD> |
|---|
| 266 | </TR> |
|---|
| 267 | </TABLE> |
|---|
| 268 | </P> |
|---|
| 269 | <H5>"Single character" character classes:</H5> |
|---|
| 270 | <P>Any escaped character <EM>x</EM>, if <EM>x</EM> is the name of a character |
|---|
| 271 | class shall match any character that is a member of that class, and any escaped |
|---|
| 272 | character <EM>X</EM>, if <EM>x</EM> is the name of a character class, shall |
|---|
| 273 | match any character not in that class.</P> |
|---|
| 274 | <P>The following are supported by default:</P> |
|---|
| 275 | <P> |
|---|
| 276 | <TABLE id="Table3" cellSpacing="1" cellPadding="1" width="300" border="1"> |
|---|
| 277 | <TR> |
|---|
| 278 | <TD><STRONG>Escape sequence</STRONG></TD> |
|---|
| 279 | <TD><STRONG>Equivalent to</STRONG></TD> |
|---|
| 280 | </TR> |
|---|
| 281 | <TR> |
|---|
| 282 | <TD>\d</TD> |
|---|
| 283 | <TD>[[:digit:]]</TD> |
|---|
| 284 | </TR> |
|---|
| 285 | <TR> |
|---|
| 286 | <TD>\l</TD> |
|---|
| 287 | <TD>[[:lower:]]</TD> |
|---|
| 288 | </TR> |
|---|
| 289 | <TR> |
|---|
| 290 | <TD>\s</TD> |
|---|
| 291 | <TD>[[:space:]]</TD> |
|---|
| 292 | </TR> |
|---|
| 293 | <TR> |
|---|
| 294 | <TD>\u</TD> |
|---|
| 295 | <TD>[[:upper:]]</TD> |
|---|
| 296 | </TR> |
|---|
| 297 | <TR> |
|---|
| 298 | <TD>\w</TD> |
|---|
| 299 | <TD>[[:word:]]</TD> |
|---|
| 300 | </TR> |
|---|
| 301 | <TR> |
|---|
| 302 | <TD>\D</TD> |
|---|
| 303 | <TD>[^[:digit:]]</TD> |
|---|
| 304 | </TR> |
|---|
| 305 | <TR> |
|---|
| 306 | <TD>\L</TD> |
|---|
| 307 | <TD>[^[:lower:]]</TD> |
|---|
| 308 | </TR> |
|---|
| 309 | <TR> |
|---|
| 310 | <TD>\S</TD> |
|---|
| 311 | <TD>[^[:space:]]</TD> |
|---|
| 312 | </TR> |
|---|
| 313 | <TR> |
|---|
| 314 | <TD>\U</TD> |
|---|
| 315 | <TD>[^[:upper:]]</TD> |
|---|
| 316 | </TR> |
|---|
| 317 | <TR> |
|---|
| 318 | <TD>\W</TD> |
|---|
| 319 | <TD>[^[:word:]]</TD> |
|---|
| 320 | </TR> |
|---|
| 321 | </TABLE> |
|---|
| 322 | </P> |
|---|
| 323 | <H5>Character Properties</H5> |
|---|
| 324 | <P>The character property names in the following table are all equivalent to the <A href="character_class_names.html"> |
|---|
| 325 | names used in character classes</A>.</P> |
|---|
| 326 | <P> |
|---|
| 327 | <TABLE id="Table9" cellSpacing="1" cellPadding="1" width="100%" border="0"> |
|---|
| 328 | <TR> |
|---|
| 329 | <TD><STRONG>Form</STRONG></TD> |
|---|
| 330 | <TD><STRONG>Description</STRONG></TD> |
|---|
| 331 | <TD><STRONG>Equivalent character set form</STRONG></TD> |
|---|
| 332 | </TR> |
|---|
| 333 | <TR> |
|---|
| 334 | <TD>\pX</TD> |
|---|
| 335 | <TD>Matches any character that has the property X.</TD> |
|---|
| 336 | <TD>[[:X:]]</TD> |
|---|
| 337 | </TR> |
|---|
| 338 | <TR> |
|---|
| 339 | <TD>\p{Name}</TD> |
|---|
| 340 | <TD>Matches any character that has the property <EM>Name</EM>.</TD> |
|---|
| 341 | <TD>[[:Name:]]</TD> |
|---|
| 342 | </TR> |
|---|
| 343 | <TR> |
|---|
| 344 | <TD>\PX</TD> |
|---|
| 345 | <TD>Matches any character that does not have the property X.</TD> |
|---|
| 346 | <TD>[^[:X:]]</TD> |
|---|
| 347 | </TR> |
|---|
| 348 | <TR> |
|---|
| 349 | <TD>\P{Name}</TD> |
|---|
| 350 | <TD>Matches any character that does not have the property <EM>Name</EM>.</TD> |
|---|
| 351 | <TD>[^[:Name:]]</TD> |
|---|
| 352 | </TR> |
|---|
| 353 | </TABLE> |
|---|
| 354 | </P> |
|---|
| 355 | <H5>Word Boundaries</H5> |
|---|
| 356 | <P>The following escape sequences match the boundaries of words:</P> |
|---|
| 357 | <P> |
|---|
| 358 | <TABLE id="Table4" cellSpacing="1" cellPadding="1" width="100%" border="1"> |
|---|
| 359 | <TR> |
|---|
| 360 | <TD>\<</TD> |
|---|
| 361 | <TD>Matches the start of a word.</TD> |
|---|
| 362 | </TR> |
|---|
| 363 | <TR> |
|---|
| 364 | <TD>\></TD> |
|---|
| 365 | <TD>Matches the end of a word.</TD> |
|---|
| 366 | </TR> |
|---|
| 367 | <TR> |
|---|
| 368 | <TD>\b</TD> |
|---|
| 369 | <TD>Matches a word boundary (the start or end of a word).</TD> |
|---|
| 370 | </TR> |
|---|
| 371 | <TR> |
|---|
| 372 | <TD>\B</TD> |
|---|
| 373 | <TD>Matches only when not at a word boundary.</TD> |
|---|
| 374 | </TR> |
|---|
| 375 | </TABLE> |
|---|
| 376 | </P> |
|---|
| 377 | <H5>Buffer boundaries</H5> |
|---|
| 378 | <P>The following match only at buffer boundaries: a "buffer" in this context is |
|---|
| 379 | the whole of the input text that is being matched against (note that ^ and |
|---|
| 380 | $ may match embedded newlines within the text).</P> |
|---|
| 381 | <P> |
|---|
| 382 | <TABLE id="Table5" cellSpacing="1" cellPadding="1" width="100%" border="1"> |
|---|
| 383 | <TR> |
|---|
| 384 | <TD>\`</TD> |
|---|
| 385 | <TD>Matches at the start of a buffer only.</TD> |
|---|
| 386 | </TR> |
|---|
| 387 | <TR> |
|---|
| 388 | <TD>\'</TD> |
|---|
| 389 | <TD>Matches at the end of a buffer only.</TD> |
|---|
| 390 | </TR> |
|---|
| 391 | <TR> |
|---|
| 392 | <TD>\A</TD> |
|---|
| 393 | <TD>Matches at the start of a buffer only (the same as \`).</TD> |
|---|
| 394 | </TR> |
|---|
| 395 | <TR> |
|---|
| 396 | <TD>\z</TD> |
|---|
| 397 | <TD>Matches at the end of a buffer only (the same as \').</TD> |
|---|
| 398 | </TR> |
|---|
| 399 | <TR> |
|---|
| 400 | <TD>\Z</TD> |
|---|
| 401 | <TD>Matches an optional sequence of newlines at the end of a buffer: equivalent to |
|---|
| 402 | the regular expression \n*\z</TD> |
|---|
| 403 | </TR> |
|---|
| 404 | </TABLE> |
|---|
| 405 | </P> |
|---|
| 406 | <H5>Continuation Escape</H5> |
|---|
| 407 | <P>The sequence \G matches only at the end of the last match found, or at the |
|---|
| 408 | start of the text being matched if no previous match was found. This |
|---|
| 409 | escape useful if you're iterating over the matches contained within a text, and |
|---|
| 410 | you want each subsequence match to start where the last one ended.</P> |
|---|
| 411 | <H5>Quoting escape</H5> |
|---|
| 412 | <P>The escape sequence \Q begins a "quoted sequence": all the subsequent |
|---|
| 413 | characters are treated as literals, until either the end of the regular |
|---|
| 414 | expression or \E is found. For example the expression: \Q\*+\Ea+ would |
|---|
| 415 | match either of:</P> |
|---|
| 416 | <PRE>\*+a<BR>\*+aaa</PRE> |
|---|
| 417 | <H5>Unicode escapes</H5> |
|---|
| 418 | <P> |
|---|
| 419 | <TABLE id="Table6" cellSpacing="1" cellPadding="1" width="100%" border="1"> |
|---|
| 420 | <TR> |
|---|
| 421 | <TD>\C</TD> |
|---|
| 422 | <TD>Matches a single code point: in Boost regex this has exactly the same effect |
|---|
| 423 | as a "." operator.</TD> |
|---|
| 424 | </TR> |
|---|
| 425 | <TR> |
|---|
| 426 | <TD>\X</TD> |
|---|
| 427 | <TD>Matches a combining character sequence: that is any non-combining character |
|---|
| 428 | followed by a sequence of zero or more combining characters.</TD> |
|---|
| 429 | </TR> |
|---|
| 430 | </TABLE> |
|---|
| 431 | </P> |
|---|
| 432 | <H5>Any other escape</H5> |
|---|
| 433 | <P>Any other escape sequence matches the character that is escaped, for example \@ |
|---|
| 434 | matches a literal <A href="mailto:'@'">'@'</A>.</P> |
|---|
| 435 | </BLOCKQUOTE> |
|---|
| 436 | <H4 dir="ltr">Perl Extended Patterns</H4> |
|---|
| 437 | <P dir="ltr">Perl-specific extensions to the regular expression syntax all start |
|---|
| 438 | with (?.</P> |
|---|
| 439 | <BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px"> |
|---|
| 440 | <H5 dir="ltr">Comments</H5> |
|---|
| 441 | <P dir="ltr">(?# ... ) is treated as a comment, it's contents are ignored.</P> |
|---|
| 442 | <H5 dir="ltr">Modifiers</H5> |
|---|
| 443 | <P dir="ltr">(?imsx-imsx ... ) alters which of the perl modifiers are in effect |
|---|
| 444 | within the pattern, changes take effect from the point that the block is first |
|---|
| 445 | seen and extend to any enclosing ). Letters before a '-' turn that perl |
|---|
| 446 | modifier on, letters afterward, turn it off.</P> |
|---|
| 447 | <P dir="ltr">(?imsx-imsx:pattern) applies the specified modifiers to <EM>pattern</EM> |
|---|
| 448 | only.</P> |
|---|
| 449 | <H5 dir="ltr">Non-marking grouping</H5> |
|---|
| 450 | <P dir="ltr">(?:pattern) lexically groups <EM>pattern</EM>, without generating an |
|---|
| 451 | additional sub-expression.</P> |
|---|
| 452 | <H5 dir="ltr">Lookahead</H5> |
|---|
| 453 | <P dir="ltr">(?=pattern) consumes zero characters, only if <EM>pattern</EM> matches.</P> |
|---|
| 454 | <P dir="ltr">(?!pattern) consumes zero characters, only if <EM>pattern</EM> does |
|---|
| 455 | not match.</P> |
|---|
| 456 | <P dir="ltr">Lookahead is typically used to create the logical AND of two regular |
|---|
| 457 | expressions, for example if a password must contain a lower case letter, an |
|---|
| 458 | upper case letter, a punctuation symbol, and be at least 6 characters long, |
|---|
| 459 | then the expression:</P> |
|---|
| 460 | <PRE dir="ltr">(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}</PRE> |
|---|
| 461 | <P dir="ltr">could be used to validate the password.</P> |
|---|
| 462 | <H5 dir="ltr">Lookbehind</H5> |
|---|
| 463 | <P dir="ltr">(?<=pattern) consumes zero characters, only if <EM>pattern</EM> could |
|---|
| 464 | be matched against the characters preceding the current position (<EM>pattern</EM> |
|---|
| 465 | must be of fixed length).</P> |
|---|
| 466 | <P dir="ltr">(?<!pattern) consumes zero characters, only if <EM>pattern</EM> could |
|---|
| 467 | not be matched against the characters preceding the current position (<EM>pattern</EM> |
|---|
| 468 | must be of fixed length).</P> |
|---|
| 469 | <H5 dir="ltr">Independent sub-expressions</H5> |
|---|
| 470 | <P dir="ltr">(?>pattern) <EM>pattern</EM> is matched independently of the |
|---|
| 471 | surrounding patterns, the expression will never backtrack into <EM>pattern</EM>. |
|---|
| 472 | Independent sub-expressions are typically used to improve performance; only the |
|---|
| 473 | best possible match for <EM>pattern</EM> will be considered, if this doesn't |
|---|
| 474 | allow the expression as a whole to match then no match is found at all.</P> |
|---|
| 475 | <H5 dir="ltr">Conditional Expressions</H5> |
|---|
| 476 | <P dir="ltr">(?(condition)yes-pattern|no-pattern) attempts to match <EM>yes-pattern</EM> |
|---|
| 477 | if the <EM>condition </EM>is true, otherwise attempts to match <EM>no-pattern</EM>.</P> |
|---|
| 478 | <P dir="ltr">(?(condition)yes-pattern) attempts to match <EM>yes-pattern</EM> if |
|---|
| 479 | the <EM>condition </EM>is true, otherwise fails.</P> |
|---|
| 480 | <P dir="ltr"><EM>Condition</EM> may be either a forward lookahead assert, or the |
|---|
| 481 | index of a marked sub-expression (the condition becomes true if the |
|---|
| 482 | sub-expression has been matched).</P> |
|---|
| 483 | </BLOCKQUOTE><A name="what"> |
|---|
| 484 | <H4>Operator precedence</H4> |
|---|
| 485 | <P> The order of precedence for of operators is as shown in the following |
|---|
| 486 | table:</P> |
|---|
| 487 | <P> |
|---|
| 488 | <TABLE id="Table2" cellSpacing="1" cellPadding="1" width="100%" border="1"> |
|---|
| 489 | <TR> |
|---|
| 490 | <TD>Collation-related bracket symbols</TD> |
|---|
| 491 | <TD>[==] [::] [..]</TD> |
|---|
| 492 | </TR> |
|---|
| 493 | <TR> |
|---|
| 494 | <TD>Escaped characters |
|---|
| 495 | </TD> |
|---|
| 496 | <TD>\</TD> |
|---|
| 497 | </TR> |
|---|
| 498 | <TR> |
|---|
| 499 | <TD>Character set (bracket expression) |
|---|
| 500 | </TD> |
|---|
| 501 | <TD>[]</TD> |
|---|
| 502 | </TR> |
|---|
| 503 | <TR> |
|---|
| 504 | <TD>Grouping</TD> |
|---|
| 505 | <TD>()</TD> |
|---|
| 506 | </TR> |
|---|
| 507 | <TR> |
|---|
| 508 | <TD>Single-character-ERE duplication |
|---|
| 509 | </TD> |
|---|
| 510 | <TD>* + ? {m,n}</TD> |
|---|
| 511 | </TR> |
|---|
| 512 | <TR> |
|---|
| 513 | <TD>Concatenation</TD> |
|---|
| 514 | <TD></TD> |
|---|
| 515 | </TR> |
|---|
| 516 | <TR> |
|---|
| 517 | <TD>Anchoring</TD> |
|---|
| 518 | <TD>^$</TD> |
|---|
| 519 | </TR> |
|---|
| 520 | <TR> |
|---|
| 521 | <TD>Alternation</TD> |
|---|
| 522 | <TD>|</TD> |
|---|
| 523 | </TR> |
|---|
| 524 | </TABLE> |
|---|
| 525 | </P> |
|---|
| 526 | </A> |
|---|
| 527 | <H3>What gets matched</H3> |
|---|
| 528 | <P>If you view the regular expression as a directed (possibly cyclic) graph, then |
|---|
| 529 | the best match found is the first match found by a depth-first-search performed |
|---|
| 530 | on that graph, while matching the input text.</P> |
|---|
| 531 | <P>Alternatively:</P> |
|---|
| 532 | <P>the best match found is the leftmost match, with individual elements matched as |
|---|
| 533 | follows;</P> |
|---|
| 534 | <P> |
|---|
| 535 | <TABLE id="Table8" cellSpacing="1" cellPadding="1" width="100%" border="0"> |
|---|
| 536 | <TR> |
|---|
| 537 | <TD><STRONG>Construct</STRONG></TD> |
|---|
| 538 | <TD><STRONG>What gets matches</STRONG></TD> |
|---|
| 539 | </TR> |
|---|
| 540 | <TR> |
|---|
| 541 | <TD>AtomA AtomB</TD> |
|---|
| 542 | <TD>Locates the best match for AtomA that has a following match for AtomB.</TD> |
|---|
| 543 | </TR> |
|---|
| 544 | <TR> |
|---|
| 545 | <TD>Expression1 | Expression2</TD> |
|---|
| 546 | <TD>If Expresion1 can be matched then returns that match, otherwise attempts to |
|---|
| 547 | match Expression2.</TD> |
|---|
| 548 | </TR> |
|---|
| 549 | <TR> |
|---|
| 550 | <TD>S{N}</TD> |
|---|
| 551 | <TD>Matches S repeated exactly N times.</TD> |
|---|
| 552 | </TR> |
|---|
| 553 | <TR> |
|---|
| 554 | <TD>S{N,M}</TD> |
|---|
| 555 | <TD>Matches S repeated between N and M times, and as many times as possible.</TD> |
|---|
| 556 | </TR> |
|---|
| 557 | <TR> |
|---|
| 558 | <TD>S{N,M}?</TD> |
|---|
| 559 | <TD>Matches S repeated between N and M times, and as few times as possible.</TD> |
|---|
| 560 | </TR> |
|---|
| 561 | <TR> |
|---|
| 562 | <TD><!--StartFragment --> S?, S*, S+</TD> |
|---|
| 563 | <TD><!--StartFragment --> The same as <CODE>S{0,1}</CODE>, <CODE>S{0,UINT_MAX}</CODE>, |
|---|
| 564 | <CODE>S{1,UINT_MAX}</CODE> respectively. |
|---|
| 565 | </TD> |
|---|
| 566 | </TR> |
|---|
| 567 | <TR> |
|---|
| 568 | <TD>S??, S*?, S+?</TD> |
|---|
| 569 | <TD>The same as <CODE>S{0,1}?</CODE>, <CODE>S{0,UINT_MAX}?</CODE>, <CODE>S{1,UINT_MAX}?</CODE> |
|---|
| 570 | respectively. |
|---|
| 571 | </TD> |
|---|
| 572 | </TR> |
|---|
| 573 | <TR> |
|---|
| 574 | <TD><!--StartFragment --> (?>S) |
|---|
| 575 | </TD> |
|---|
| 576 | <TD>Matches the best match for S, and only that.</TD> |
|---|
| 577 | </TR> |
|---|
| 578 | <TR> |
|---|
| 579 | <TD> |
|---|
| 580 | (?=S), (?<=S) |
|---|
| 581 | </TD> |
|---|
| 582 | <TD>Matches only the best match for S (this is only visible if there are capturing |
|---|
| 583 | parenthesis within S).</TD> |
|---|
| 584 | </TR> |
|---|
| 585 | <TR> |
|---|
| 586 | <TD><!--StartFragment --> (?!S), (?<!S)</TD> |
|---|
| 587 | <TD>Considers only whether a match for S exists or not.</TD> |
|---|
| 588 | </TR> |
|---|
| 589 | <TR> |
|---|
| 590 | <TD><!--StartFragment --> (?(condition)yes-pattern | no-pattern)</TD> |
|---|
| 591 | <TD>If condition is <EM>true</EM>, then only <EM>yes-pattern</EM> is considered, |
|---|
| 592 | otherwise only <EM>no-pattern</EM> is considered.</TD> |
|---|
| 593 | </TR> |
|---|
| 594 | </TABLE> |
|---|
| 595 | </P> |
|---|
| 596 | <H3><A name="variations"></A>Variations</H3> |
|---|
| 597 | <P>The options <A href="syntax_option_type.html#perl"><EM>normal, ECMAScript, JavaScript</EM> |
|---|
| 598 | and <EM>JScript</EM></A> are all synonyms for <EM>Perl</EM>.</P> |
|---|
| 599 | <H3><A name="options"></A>Options</H3> |
|---|
| 600 | <P>There are a <A href="syntax_option_type.html#Perl">variety of flags</A> that |
|---|
| 601 | may be combined with the <EM>Perl</EM> option when constructing the regular |
|---|
| 602 | expression, in particular note that the <A href="syntax_option_type.html#Perl">newline_alt</A> |
|---|
| 603 | option alters the syntax, while the <A href="syntax_option_type.html#Perl">collate, |
|---|
| 604 | nosubs and icase</A> options modify how the case and locale sensitivity |
|---|
| 605 | are to be applied.</P> |
|---|
| 606 | <H3><A name="mods"></A>Modifiers</H3> |
|---|
| 607 | <P>The perl <EM>smix</EM> modifiers can either be applied using a (?smix-smix) |
|---|
| 608 | prefix to the regular expression, or with one of the regex-compile time flags <EM><A href="syntax_option_type.html#Perl"> |
|---|
| 609 | no_mod_m, mod_x, mod_s, and no_mod_s</A></EM>. |
|---|
| 610 | </P> |
|---|
| 611 | <H3><A name="refs">References</H3> |
|---|
| 612 | <P><A href="http://perldoc.perl.org/perlre.html"> Perl 5.8.</A></P> |
|---|
| 613 | <HR> |
|---|
| 614 | <P></P> |
|---|
| 615 | <p>Revised |
|---|
| 616 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
|---|
| 617 | 21 Aug 2004 |
|---|
| 618 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --></p> |
|---|
| 619 | <P><I>© Copyright <a href="mailto:jm@regex.fsnet.co.uk">John Maddock</a> 2004</I></P> |
|---|
| 620 | <I> |
|---|
| 621 | <P><I>Use, modification and distribution are subject to the Boost Software License, |
|---|
| 622 | Version 1.0. (See accompanying file <A href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> |
|---|
| 623 | or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>).</I></P> |
|---|
| 624 | </I> |
|---|
| 625 | </body> |
|---|
| 626 | </html> |
|---|