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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Perl Regular Expression Syntax</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Boost.Regex 5.1.2">
<link rel="up" href="../syntax.html" title="Regular Expression Syntax">
<link rel="prev" href="../syntax.html" title="Regular Expression Syntax">
<link rel="next" href="basic_extended.html" title="POSIX Extended Regular Expression Syntax">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../syntax.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../syntax.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="basic_extended.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_regex.syntax.perl_syntax"></a><a class="link" href="perl_syntax.html" title="Perl Regular Expression Syntax">Perl Regular Expression
Syntax</a>
</h3></div></div></div>
<h4>
<a name="boost_regex.syntax.perl_syntax.h0"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.synopsis"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.synopsis">Synopsis</a>
</h4>
<p>
The Perl regular expression syntax is based on that used by the programming
language Perl . Perl regular expressions are the default behavior in Boost.Regex
or you can pass the flag <code class="literal">perl</code> to the <a class="link" href="../ref/basic_regex.html" title="basic_regex"><code class="computeroutput"><span class="identifier">basic_regex</span></code></a> constructor, for example:
</p>
<pre class="programlisting"><span class="comment">// e1 is a case sensitive Perl regular expression: </span>
<span class="comment">// since Perl is the default option there's no need to explicitly specify the syntax used here:</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">e1</span><span class="special">(</span><span class="identifier">my_expression</span><span class="special">);</span>
<span class="comment">// e2 a case insensitive Perl regular expression:</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">e2</span><span class="special">(</span><span class="identifier">my_expression</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span><span class="special">::</span><span class="identifier">perl</span><span class="special">|</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span><span class="special">::</span><span class="identifier">icase</span><span class="special">);</span>
</pre>
<h4>
<a name="boost_regex.syntax.perl_syntax.h1"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.perl_regular_expression_syntax"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.perl_regular_expression_syntax">Perl
Regular Expression Syntax</a>
</h4>
<p>
In Perl regular expressions, all characters match themselves except for the
following special characters:
</p>
<pre class="programlisting">.[{}()\*+?|^$</pre>
<h5>
<a name="boost_regex.syntax.perl_syntax.h2"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.wildcard"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.wildcard">Wildcard</a>
</h5>
<p>
The single character '.' when used outside of a character set will match
any single character except:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
The NULL character when the <a class="link" href="../ref/match_flag_type.html" title="match_flag_type">flag
<code class="literal">match_not_dot_null</code></a> is passed to the matching
algorithms.
</li>
<li class="listitem">
The newline character when the <a class="link" href="../ref/match_flag_type.html" title="match_flag_type">flag
<code class="literal">match_not_dot_newline</code></a> is passed to the matching
algorithms.
</li>
</ul></div>
<h5>
<a name="boost_regex.syntax.perl_syntax.h3"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.anchors"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.anchors">Anchors</a>
</h5>
<p>
A '^' character shall match the start of a line.
</p>
<p>
A '$' character shall match the end of a line.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h4"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.marked_sub_expressions"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.marked_sub_expressions">Marked sub-expressions</a>
</h5>
<p>
A section beginning <code class="literal">(</code> and ending <code class="literal">)</code>
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>
<h5>
<a name="boost_regex.syntax.perl_syntax.h5"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.non_marking_grouping"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.non_marking_grouping">Non-marking
grouping</a>
</h5>
<p>
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 <code class="literal">(?:</code> and <code class="literal">)</code>
, for example <code class="literal">(?:ab)+</code> will repeat <code class="literal">ab</code>
without splitting out any separate sub-expressions.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h6"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.repeats"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.repeats">Repeats</a>
</h5>
<p>
Any atom (a single character, a marked sub-expression, or a character class)
can be repeated with the <code class="literal">*</code>, <code class="literal">+</code>, <code class="literal">?</code>,
and <code class="literal">{}</code> operators.
</p>
<p>
The <code class="literal">*</code> operator will match the preceding atom zero or more
times, for example the expression <code class="literal">a*b</code> will match any of
the following:
</p>
<pre class="programlisting"><span class="identifier">b</span>
<span class="identifier">ab</span>
<span class="identifier">aaaaaaaab</span>
</pre>
<p>
The <code class="literal">+</code> operator will match the preceding atom one or more
times, for example the expression <code class="literal">a+b</code> will match any of
the following:
</p>
<pre class="programlisting"><span class="identifier">ab</span>
<span class="identifier">aaaaaaaab</span>
</pre>
<p>
But will not match:
</p>
<pre class="programlisting"><span class="identifier">b</span>
</pre>
<p>
The <code class="literal">?</code> operator will match the preceding atom zero or one
times, for example the expression ca?b will match any of the following:
</p>
<pre class="programlisting"><span class="identifier">cb</span>
<span class="identifier">cab</span>
</pre>
<p>
But will not match:
</p>
<pre class="programlisting"><span class="identifier">caab</span>
</pre>
<p>
An atom can also be repeated with a bounded repeat:
</p>
<p>
<code class="literal">a{n}</code> Matches 'a' repeated exactly n times.
</p>
<p>
<code class="literal">a{n,}</code> Matches 'a' repeated n or more times.
</p>
<p>
<code class="literal">a{n, m}</code> Matches 'a' repeated between n and m times inclusive.
</p>
<p>
For example:
</p>
<pre class="programlisting">^a{2,3}$</pre>
<p>
Will match either of:
</p>
<pre class="programlisting"><span class="identifier">aa</span>
<span class="identifier">aaa</span>
</pre>
<p>
But neither of:
</p>
<pre class="programlisting"><span class="identifier">a</span>
<span class="identifier">aaaa</span>
</pre>
<p>
Note that the "{" and "}" characters will treated as
ordinary literals when used in a context that is not a repeat: this matches
Perl 5.x behavior. For example in the expressions "ab{1", "ab1}"
and "a{b}c" the curly brackets are all treated as literals and
<span class="emphasis"><em>no error will be raised</em></span>.
</p>
<p>
It is an error to use a repeat operator, if the preceding construct can not
be repeated, for example:
</p>
<pre class="programlisting"><span class="identifier">a</span><span class="special">(*)</span>
</pre>
<p>
Will raise an error, as there is nothing for the <code class="literal">*</code> operator
to be applied to.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h7"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.non_greedy_repeats"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.non_greedy_repeats">Non
greedy repeats</a>
</h5>
<p>
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.
</p>
<p>
<code class="literal">*?</code> Matches the previous atom zero or more times, while
consuming as little input as possible.
</p>
<p>
<code class="literal">+?</code> Matches the previous atom one or more times, while
consuming as little input as possible.
</p>
<p>
<code class="literal">??</code> Matches the previous atom zero or one times, while
consuming as little input as possible.
</p>
<p>
<code class="literal">{n,}?</code> Matches the previous atom n or more times, while
consuming as little input as possible.
</p>
<p>
<code class="literal">{n,m}?</code> Matches the previous atom between n and m times,
while consuming as little input as possible.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h8"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.possessive_repeats"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.possessive_repeats">Possessive
repeats</a>
</h5>
<p>
By default when a repeated pattern does not match then the engine will backtrack
until a match is found. However, this behaviour can sometime be undesireble
so there are also "possessive" repeats: these match as much as
possible and do not then allow backtracking if the rest of the expression
fails to match.
</p>
<p>
<code class="literal">*+</code> Matches the previous atom zero or more times, while
giving nothing back.
</p>
<p>
<code class="literal">++</code> Matches the previous atom one or more times, while
giving nothing back.
</p>
<p>
<code class="literal">?+</code> Matches the previous atom zero or one times, while
giving nothing back.
</p>
<p>
<code class="literal">{n,}+</code> Matches the previous atom n or more times, while
giving nothing back.
</p>
<p>
<code class="literal">{n,m}+</code> Matches the previous atom between n and m times,
while giving nothing back.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h9"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.back_references"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.back_references">Back
references</a>
</h5>
<p>
An escape character followed by a digit <span class="emphasis"><em>n</em></span>, where <span class="emphasis"><em>n</em></span>
is in the range 1-9, matches the same string that was matched by sub-expression
<span class="emphasis"><em>n</em></span>. For example the expression:
</p>
<pre class="programlisting">^(a*).*\1$</pre>
<p>
Will match the string:
</p>
<pre class="programlisting"><span class="identifier">aaabbaaa</span>
</pre>
<p>
But not the string:
</p>
<pre class="programlisting"><span class="identifier">aaabba</span>
</pre>
<p>
You can also use the \g escape for the same function, for example:
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Escape
</p>
</th>
<th>
<p>
Meaning
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="literal">\g1</code>
</p>
</td>
<td>
<p>
Match whatever matched sub-expression 1
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\g{1}</code>
</p>
</td>
<td>
<p>
Match whatever matched sub-expression 1: this form allows for safer
parsing of the expression in cases like <code class="literal">\g{1}2</code>
or for indexes higher than 9 as in <code class="literal">\g{1234}</code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\g-1</code>
</p>
</td>
<td>
<p>
Match whatever matched the last opened sub-expression
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\g{-2}</code>
</p>
</td>
<td>
<p>
Match whatever matched the last but one opened sub-expression
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\g{one}</code>
</p>
</td>
<td>
<p>
Match whatever matched the sub-expression named "one"
</p>
</td>
</tr>
</tbody>
</table></div>
<p>
Finally the \k escape can be used to refer to named subexpressions, for example
<code class="literal">\k<two></code> will match whatever matched the subexpression
named "two".
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h10"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.alternation"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.alternation">Alternation</a>
</h5>
<p>
The <code class="literal">|</code> operator will match either of its arguments, so
for example: <code class="literal">abc|def</code> will match either "abc"
or "def".
</p>
<p>
Parenthesis can be used to group alternations, for example: <code class="literal">ab(d|ef)</code>
will match either of "abd" or "abef".
</p>
<p>
Empty alternatives are not allowed (these are almost always a mistake), but
if you really want an empty alternative use <code class="literal">(?:)</code> as a
placeholder, for example:
</p>
<p>
<code class="literal">|abc</code> is not a valid expression, but
</p>
<p>
<code class="literal">(?:)|abc</code> is and is equivalent, also the expression:
</p>
<p>
<code class="literal">(?:abc)??</code> has exactly the same effect.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h11"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.character_sets"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.character_sets">Character
sets</a>
</h5>
<p>
A character set is a bracket-expression starting with <code class="literal">[] and ending
with <code class="literal"></code></code>, 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>
<h6>
<a name="boost_regex.syntax.perl_syntax.h12"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.single_characters"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.single_characters">Single
characters</a>
</h6>
<p>
For example <code class="literal">[abc]</code>, will match any of the characters 'a',
'b', or 'c'.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h13"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.character_ranges"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.character_ranges">Character
ranges</a>
</h6>
<p>
For example <code class="literal">[a-c]</code> will match any single character in the
range 'a' to 'c'. 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
<a class="link" href="../ref/syntax_option_type/syntax_option_type_perl.html" title="Options for Perl Regular Expressions"><code class="literal">collate</code>
flag</a> when constructing the regular expression, then ranges are locale
sensitive.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h14"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.negation"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.negation">Negation</a>
</h6>
<p>
If the bracket-expression begins with the ^ character, then it matches the
complement of the characters it contains, for example <code class="literal">[^a-c]</code>
matches any character that is not in the range <code class="literal">a-c</code>.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h15"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.character_classes"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.character_classes">Character
classes</a>
</h6>
<p>
An expression of the form <code class="literal">[[:name:]]</code> matches the named
character class "name", for example <code class="literal">[[:lower:]]</code>
matches any lower case character. See <a class="link" href="character_classes.html" title="Character Class Names">character
class names</a>.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h16"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.collating_elements"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.collating_elements">Collating
Elements</a>
</h6>
<p>
An expression of the form <code class="literal">[[.col.]]</code> matches the collating
element <span class="emphasis"><em>col</em></span>. 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: <code class="literal">[[.ae.]-c]</code>
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>
As an extension, a collating element may also be specified via it's <a class="link" href="collating_names.html" title="Collating Names">symbolic name</a>, for example:
</p>
<pre class="programlisting"><span class="special">[[.</span><span class="identifier">NUL</span><span class="special">.]]</span>
</pre>
<p>
matches a <code class="literal">\0</code> character.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h17"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.equivalence_classes"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.equivalence_classes">Equivalence
classes</a>
</h6>
<p>
An expression of the form <code class="literal">[[=col=]]</code>, matches any character
or collating element whose primary sort key is the same as that for collating
element <span class="emphasis"><em>col</em></span>, as with collating elements the name <span class="emphasis"><em>col</em></span>
may be a <a class="link" href="collating_names.html" title="Collating Names">symbolic name</a>.
A primary sort key is one that ignores case, accentation, or locale-specific
tailorings; so for example <code class="computeroutput"><span class="special">[[=</span><span class="identifier">a</span><span class="special">=]]</span></code> 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>
<h6>
<a name="boost_regex.syntax.perl_syntax.h18"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.escaped_characters"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.escaped_characters">Escaped
Characters</a>
</h6>
<p>
All the escape sequences that match a single character, or a single character
class are permitted within a character class definition. For example <code class="computeroutput"><span class="special">[\[\]]</span></code> would match either of <code class="computeroutput"><span class="special">[</span></code> or <code class="computeroutput"><span class="special">]</span></code>
while <code class="computeroutput"><span class="special">[\</span><span class="identifier">W</span><span class="special">\</span><span class="identifier">d</span><span class="special">]</span></code>
would match any character that is either a "digit", <span class="emphasis"><em>or</em></span>
is <span class="emphasis"><em>not</em></span> a "word" character.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h19"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.combinations"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.combinations">Combinations</a>
</h6>
<p>
All of the above can be combined in one character set declaration, for example:
<code class="literal">[[:digit:]a-c[.NUL.]]</code>.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h20"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.escapes"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.escapes">Escapes</a>
</h5>
<p>
Any special character preceded by an escape shall match itself.
</p>
<p>
The following escape sequences are all synonyms for single characters:
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Escape
</p>
</th>
<th>
<p>
Character
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="literal">\a</code>
</p>
</td>
<td>
<p>
<code class="literal">\a</code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\e</code>
</p>
</td>
<td>
<p>
<code class="literal">0x1B</code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\f</code>
</p>
</td>
<td>
<p>
<code class="literal">\f</code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><br> </code>
</p>
</td>
<td>
<p>
<code class="literal"><br> </code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\r</code>
</p>
</td>
<td>
<p>
<code class="literal">\r</code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\t</code>
</p>
</td>
<td>
<p>
<code class="literal">\t</code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\v</code>
</p>
</td>
<td>
<p>
<code class="literal">\v</code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\b</code>
</p>
</td>
<td>
<p>
<code class="literal">\b</code> (but only inside a character class declaration).
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\cX</code>
</p>
</td>
<td>
<p>
An ASCII escape sequence - the character whose code point is X
% 32
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\xdd</code>
</p>
</td>
<td>
<p>
A hexadecimal escape sequence - matches the single character whose
code point is 0xdd.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\x{dddd}</code>
</p>
</td>
<td>
<p>
A hexadecimal escape sequence - matches the single character whose
code point is 0xdddd.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\0ddd</code>
</p>
</td>
<td>
<p>
An octal escape sequence - matches the single character whose code
point is 0ddd.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">\N{name}</code>
</p>
</td>
<td>
<p>
Matches the single character which has the <a class="link" href="collating_names.html" title="Collating Names">symbolic
name</a> <span class="emphasis"><em>name</em></span>. For example <code class="literal">\N{newline}</code>
matches the single character \n.
</p>
</td>
</tr>
</tbody>
</table></div>
<h6>
<a name="boost_regex.syntax.perl_syntax.h21"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.single_character_character_class"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.single_character_character_class">"Single
character" character classes:</a>
</h6>
<p>
Any escaped character <span class="emphasis"><em>x</em></span>, if <span class="emphasis"><em>x</em></span> is
the name of a character class shall match any character that is a member
of that class, and any escaped character <span class="emphasis"><em>X</em></span>, if <span class="emphasis"><em>x</em></span>
is the name of a character class, shall match any character not in that class.
</p>
<p>
The following are supported by default:
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Escape sequence
</p>
</th>
<th>
<p>
Equivalent to
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">d</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[[:</span><span class="identifier">digit</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">l</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[[:</span><span class="identifier">lower</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">s</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[[:</span><span class="identifier">space</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">u</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[[:</span><span class="identifier">upper</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">w</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[[:</span><span class="identifier">word</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">h</span></code>
</p>
</td>
<td>
<p>
Horizontal whitespace
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">v</span></code>
</p>
</td>
<td>
<p>
Vertical whitespace
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">D</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[^[:</span><span class="identifier">digit</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">L</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[^[:</span><span class="identifier">lower</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">S</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[^[:</span><span class="identifier">space</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">U</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[^[:</span><span class="identifier">upper</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">W</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[^[:</span><span class="identifier">word</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">H</span></code>
</p>
</td>
<td>
<p>
Not Horizontal whitespace
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">V</span></code>
</p>
</td>
<td>
<p>
Not Vertical whitespace
</p>
</td>
</tr>
</tbody>
</table></div>
<h6>
<a name="boost_regex.syntax.perl_syntax.h22"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.character_properties"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.character_properties">Character
Properties</a>
</h6>
<p>
The character property names in the following table are all equivalent to
the <a class="link" href="character_classes.html" title="Character Class Names">names used in character
classes</a>.
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Form
</p>
</th>
<th>
<p>
Description
</p>
</th>
<th>
<p>
Equivalent character set form
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">pX</span></code>
</p>
</td>
<td>
<p>
Matches any character that has the property X.
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[[:</span><span class="identifier">X</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">p</span><span class="special">{</span><span class="identifier">Name</span><span class="special">}</span></code>
</p>
</td>
<td>
<p>
Matches any character that has the property Name.
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[[:</span><span class="identifier">Name</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">PX</span></code>
</p>
</td>
<td>
<p>
Matches any character that does not have the property X.
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[^[:</span><span class="identifier">X</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="special">\</span><span class="identifier">P</span><span class="special">{</span><span class="identifier">Name</span><span class="special">}</span></code>
</p>
</td>
<td>
<p>
Matches any character that does not have the property Name.
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="special">[^[:</span><span class="identifier">Name</span><span class="special">:]]</span></code>
</p>
</td>
</tr>
</tbody>
</table></div>
<p>
For example <code class="literal">\pd</code> matches any "digit" character,
as does <code class="literal">\p{digit}</code>.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h23"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.word_boundaries"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.word_boundaries">Word
Boundaries</a>
</h6>
<p>
The following escape sequences match the boundaries of words:
</p>
<p>
<code class="literal"><</code> Matches the start of a word.
</p>
<p>
<code class="literal">></code> Matches the end of a word.
</p>
<p>
<code class="literal">\b</code> Matches a word boundary (the start or end of a word).
</p>
<p>
<code class="literal">\B</code> Matches only when not at a word boundary.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h24"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.buffer_boundaries"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.buffer_boundaries">Buffer
boundaries</a>
</h6>
<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>
\` Matches at the start of a buffer only.
</p>
<p>
\' Matches at the end of a buffer only.
</p>
<p>
\A Matches at the start of a buffer only (the same as <code class="literal">\`</code>).
</p>
<p>
\z Matches at the end of a buffer only (the same as <code class="literal">\'</code>).
</p>
<p>
\Z Matches a zero-width assertion consisting of an optional sequence of newlines
at the end of a buffer: equivalent to the regular expression <code class="literal">(?=\v*\z)</code>.
Note that this is subtly different from Perl which behaves as if matching
<code class="literal">(?=\n?\z)</code>.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h25"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.continuation_escape"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.continuation_escape">Continuation
Escape</a>
</h6>
<p>
The sequence <code class="literal">\G</code> 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>
<h6>
<a name="boost_regex.syntax.perl_syntax.h26"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.quoting_escape"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.quoting_escape">Quoting
escape</a>
</h6>
<p>
The escape sequence <code class="literal">\Q</code> 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: <code class="literal">\Q*+\Ea+</code>
would match either of:
</p>
<pre class="programlisting"><span class="special">\*+</span><span class="identifier">a</span>
<span class="special">\*+</span><span class="identifier">aaa</span>
</pre>
<h6>
<a name="boost_regex.syntax.perl_syntax.h27"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.unicode_escapes"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.unicode_escapes">Unicode
escapes</a>
</h6>
<p>
<code class="literal">\C</code> Matches a single code point: in Boost regex this has
exactly the same effect as a "." operator. <code class="literal">\X</code>
Matches a combining character sequence: that is any non-combining character
followed by a sequence of zero or more combining characters.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h28"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.matching_line_endings"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.matching_line_endings">Matching Line
Endings</a>
</h6>
<p>
The escape sequence <code class="literal">\R</code> matches any line ending character
sequence, specifically it is identical to the expression <code class="literal">(?>\x0D\x0A?|[\x0A-\x0C\x85\x{2028}\x{2029}])</code>.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h29"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.keeping_back_some_text"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.keeping_back_some_text">Keeping back
some text</a>
</h6>
<p>
<code class="literal">\K</code> 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.
</p>
<p>
For example <code class="literal">foo\Kbar</code> 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.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h30"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.any_other_escape"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.any_other_escape">Any
other escape</a>
</h6>
<p>
Any other escape sequence matches the character that is escaped, for example
\@ matches a literal '@'.
</p>
<h5>
<a name="boost_regex.syntax.perl_syntax.h31"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.perl_extended_patterns"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.perl_extended_patterns">Perl Extended
Patterns</a>
</h5>
<p>
Perl-specific extensions to the regular expression syntax all start with
<code class="literal">(?</code>.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h32"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.named_subexpressions"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.named_subexpressions">Named
Subexpressions</a>
</h6>
<p>
You can create a named subexpression using:
</p>
<pre class="programlisting"><span class="special">(?<</span><span class="identifier">NAME</span><span class="special">></span><span class="identifier">expression</span><span class="special">)</span>
</pre>
<p>
Which can be then be referred to by the name <span class="emphasis"><em>NAME</em></span>. Alternatively
you can delimit the name using 'NAME' as in:
</p>
<pre class="programlisting"><span class="special">(?</span><span class="char">'NAME'</span><span class="identifier">expression</span><span class="special">)</span>
</pre>
<p>
These named subexpressions can be referred to in a backreference using either
<code class="literal">\g{NAME}</code> or <code class="literal">\k<NAME></code> and can
also be referred to by name in a <a class="link" href="../format/perl_format.html" title="Perl Format String Syntax">Perl</a>
format string for search and replace operations, or in the <a class="link" href="../ref/match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a> member functions.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h33"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.comments"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.comments">Comments</a>
</h6>
<p>
<code class="literal">(?# ... )</code> is treated as a comment, it's contents are ignored.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h34"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.modifiers"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.modifiers">Modifiers</a>
</h6>
<p>
<code class="literal">(?imsx-imsx ... )</code> alters which of the perl modifiers are
in effect within the pattern, changes take effect from the point that the
block is first seen and extend to any enclosing <code class="literal">)</code>. Letters
before a '-' turn that perl modifier on, letters afterward, turn it off.
</p>
<p>
<code class="literal">(?imsx-imsx:pattern)</code> applies the specified modifiers to
pattern only.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h35"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.non_marking_groups"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.non_marking_groups">Non-marking
groups</a>
</h6>
<p>
<code class="literal">(?:pattern)</code> lexically groups pattern, without generating
an additional sub-expression.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h36"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.branch_reset"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.branch_reset">Branch
reset</a>
</h6>
<p>
<code class="literal">(?|pattern)</code> resets the subexpression count at the start
of each "|" alternative within <span class="emphasis"><em>pattern</em></span>.
</p>
<p>
The sub-expression count following this construct is that of whichever branch
had the largest number of sub-expressions. This construct is useful when
you want to capture one of a number of alternative matches in a single sub-expression
index.
</p>
<p>
In the following example the index of each sub-expression is shown below
the expression:
</p>
<pre class="programlisting"># before ---------------branch-reset----------- after
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
# 1 2 2 3 2 3 4
</pre>
<h6>
<a name="boost_regex.syntax.perl_syntax.h37"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.lookahead"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.lookahead">Lookahead</a>
</h6>
<p>
<code class="literal">(?=pattern)</code> consumes zero characters, only if pattern
matches.
</p>
<p>
<code class="literal">(?!pattern)</code> consumes zero characters, only if pattern
does not match.
</p>
<p>
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:
</p>
<pre class="programlisting"><span class="special">(?=.*[[:</span><span class="identifier">lower</span><span class="special">:]])(?=.*[[:</span><span class="identifier">upper</span><span class="special">:]])(?=.*[[:</span><span class="identifier">punct</span><span class="special">:]]).{</span><span class="number">6</span><span class="special">,}</span>
</pre>
<p>
could be used to validate the password.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h38"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.lookbehind"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.lookbehind">Lookbehind</a>
</h6>
<p>
<code class="literal">(?<=pattern)</code> consumes zero characters, only if pattern
could be matched against the characters preceding the current position (pattern
must be of fixed length).
</p>
<p>
<code class="literal">(?<!pattern)</code> consumes zero characters, only if pattern
could not be matched against the characters preceding the current position
(pattern must be of fixed length).
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h39"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.independent_sub_expressions"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.independent_sub_expressions">Independent
sub-expressions</a>
</h6>
<p>
<code class="literal">(?>pattern)</code> <span class="emphasis"><em>pattern</em></span> is matched
independently of the surrounding patterns, the expression will never backtrack
into <span class="emphasis"><em>pattern</em></span>. 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.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h40"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.recursive_expressions"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.recursive_expressions">Recursive
Expressions</a>
</h6>
<p>
<code class="literal">(?<span class="emphasis"><em>N</em></span>) (?-<span class="emphasis"><em>N</em></span>) (?+<span class="emphasis"><em>N</em></span>)
(?R) (?0) (?&NAME)</code>
</p>
<p>
<code class="literal">(?R)</code> and <code class="literal">(?0)</code> recurse to the start
of the entire pattern.
</p>
<p>
<code class="literal">(?<span class="emphasis"><em>N</em></span>)</code> executes sub-expression <span class="emphasis"><em>N</em></span>
recursively, for example <code class="literal">(?2)</code> will recurse to sub-expression
2.
</p>
<p>
<code class="literal">(?-<span class="emphasis"><em>N</em></span>)</code> and <code class="literal">(?+<span class="emphasis"><em>N</em></span>)</code>
are relative recursions, so for example <code class="literal">(?-1)</code> recurses
to the last sub-expression to be declared, and <code class="literal">(?+1)</code> recurses
to the next sub-expression to be declared.
</p>
<p>
<code class="literal">(?&NAME)</code> recurses to named sub-expression <span class="emphasis"><em>NAME</em></span>.
</p>
<h6>
<a name="boost_regex.syntax.perl_syntax.h41"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.conditional_expressions"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.conditional_expressions">Conditional
Expressions</a>
</h6>
<p>
<code class="literal">(?(condition)yes-pattern|no-pattern)</code> attempts to match
<span class="emphasis"><em>yes-pattern</em></span> if the <span class="emphasis"><em>condition</em></span> is
true, otherwise attempts to match <span class="emphasis"><em>no-pattern</em></span>.
</p>
<p>
<code class="literal">(?(condition)yes-pattern)</code> attempts to match <span class="emphasis"><em>yes-pattern</em></span>
if the <span class="emphasis"><em>condition</em></span> is true, otherwise matches the NULL
string.
</p>
<p>
<span class="emphasis"><em>condition</em></span> may be either: a forward lookahead assert,
the index of a marked sub-expression (the condition becomes true if the sub-expression
has been matched), or an index of a recursion (the condition become true
if we are executing directly inside the specified recursion).
</p>
<p>
Here is a summary of the possible predicates:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<code class="literal">(?(?=assert)yes-pattern|no-pattern)</code> Executes <span class="emphasis"><em>yes-pattern</em></span>
if the forward look-ahead assert matches, otherwise executes <span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?(?!assert)yes-pattern|no-pattern)</code> Executes <span class="emphasis"><em>yes-pattern</em></span>
if the forward look-ahead assert does not match, otherwise executes
<span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?(<span class="emphasis"><em>N</em></span>)yes-pattern|no-pattern)</code>
Executes <span class="emphasis"><em>yes-pattern</em></span> if subexpression <span class="emphasis"><em>N</em></span>
has been matched, otherwise executes <span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?(<<span class="emphasis"><em>name</em></span>>)yes-pattern|no-pattern)</code>
Executes <span class="emphasis"><em>yes-pattern</em></span> if named subexpression <span class="emphasis"><em>name</em></span>
has been matched, otherwise executes <span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?('<span class="emphasis"><em>name</em></span>')yes-pattern|no-pattern)</code>
Executes <span class="emphasis"><em>yes-pattern</em></span> if named subexpression <span class="emphasis"><em>name</em></span>
has been matched, otherwise executes <span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?(R)yes-pattern|no-pattern)</code> Executes <span class="emphasis"><em>yes-pattern</em></span>
if we are executing inside a recursion, otherwise executes <span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?(R<span class="emphasis"><em>N</em></span>)yes-pattern|no-pattern)</code>
Executes <span class="emphasis"><em>yes-pattern</em></span> if we are executing inside
a recursion to sub-expression <span class="emphasis"><em>N</em></span>, otherwise executes
<span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?(R&<span class="emphasis"><em>name</em></span>)yes-pattern|no-pattern)</code>
Executes <span class="emphasis"><em>yes-pattern</em></span> if we are executing inside
a recursion to named sub-expression <span class="emphasis"><em>name</em></span>, otherwise
executes <span class="emphasis"><em>no-pattern</em></span>.
</li>
<li class="listitem">
<code class="literal">(?(DEFINE)never-exectuted-pattern)</code> Defines a block
of code that is never executed and matches no characters: this is usually
used to define one or more named sub-expressions which are referred to
from elsewhere in the pattern.
</li>
</ul></div>
<h6>
<a name="boost_regex.syntax.perl_syntax.h42"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.backtracking_control_verbs"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.backtracking_control_verbs">Backtracking
Control Verbs</a>
</h6>
<p>
This library has partial support for Perl's backtracking control verbs, in
particular (*MARK) is not supported. There may also be detail differences
in behaviour between this library and Perl, not least because Perl's behaviour
is rather under-documented and often somewhat random in how it behaves in
practice. The verbs supported are:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<code class="literal">(*PRUNE)</code> Has no effect unless backtracked onto, in
which case all the backtracking information prior to this point is discarded.
</li>
<li class="listitem">
<code class="literal">(*SKIP)</code> Behaves the same as <code class="literal">(*PRUNE)</code>
except that it is assumed that no match can possibly occur prior to the
current point in the string being searched. This can be used to optimize
searches by skipping over chunks of text that have already been determined
can not form a match.
</li>
<li class="listitem">
<code class="literal">(*THEN)</code> Has no effect unless backtracked onto, in
which case all subsequent alternatives in a group of alternations are
discarded.
</li>
<li class="listitem">
<code class="literal">(*COMMIT)</code> Has no effect unless backtracked onto, in
which case all subsequent matching/searching attempts are abandoned.
</li>
<li class="listitem">
<code class="literal">(*FAIL)</code> Causes the match to fail unconditionally at
this point, can be used to force the engine to backtrack.
</li>
<li class="listitem">
<code class="literal">(*ACCEPT)</code> Causes the pattern to be considered matched
at the current point. Any half-open sub-expressions are closed at the
current point.
</li>
</ul></div>
<h5>
<a name="boost_regex.syntax.perl_syntax.h43"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.operator_precedence"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.operator_precedence">Operator
precedence</a>
</h5>
<p>
The order of precedence for of operators is as follows:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
Collation-related bracket symbols <code class="computeroutput"><span class="special">[==]</span>
<span class="special">[::]</span> <span class="special">[..]</span></code>
</li>
<li class="listitem">
Escaped characters <code class="literal">\</code>
</li>
<li class="listitem">
Character set (bracket expression) <code class="computeroutput"><span class="special">[]</span></code>
</li>
<li class="listitem">
Grouping <code class="literal">()</code>
</li>
<li class="listitem">
Single-character-ERE duplication <code class="literal">* + ? {m,n}</code>
</li>
<li class="listitem">
Concatenation
</li>
<li class="listitem">
Anchoring ^$
</li>
<li class="listitem">
Alternation |
</li>
</ol></div>
<h4>
<a name="boost_regex.syntax.perl_syntax.h44"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.what_gets_matched"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.what_gets_matched">What
gets matched</a>
</h4>
<p>
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.
</p>
<p>
Alternatively:
</p>
<p>
The best match found is the <a class="link" href="leftmost_longest_rule.html" title="The Leftmost Longest Rule">leftmost
match</a>, with individual elements matched as follows;
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Construct
</p>
</th>
<th>
<p>
What gets matched
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="literal">AtomA AtomB</code>
</p>
</td>
<td>
<p>
Locates the best match for <span class="emphasis"><em>AtomA</em></span> that has
a following match for <span class="emphasis"><em>AtomB</em></span>.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">Expression1 | Expression2</code>
</p>
</td>
<td>
<p>
If <span class="emphasis"><em>Expresion1</em></span> can be matched then returns
that match, otherwise attempts to match <span class="emphasis"><em>Expression2</em></span>.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">S{N}</code>
</p>
</td>
<td>
<p>
Matches <span class="emphasis"><em>S</em></span> repeated exactly N times.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">S{N,M}</code>
</p>
</td>
<td>
<p>
Matches S repeated between N and M times, and as many times as
possible.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">S{N,M}?</code>
</p>
</td>
<td>
<p>
Matches S repeated between N and M times, and as few times as possible.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">S?, S*, S+</code>
</p>
</td>
<td>
<p>
The same as <code class="literal">S{0,1}</code>, <code class="literal">S{0,UINT_MAX}</code>,
<code class="literal">S{1,UINT_MAX}</code> respectively.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">S??, S*?, S+?</code>
</p>
</td>
<td>
<p>
The same as <code class="literal">S{0,1}?</code>, <code class="literal">S{0,UINT_MAX}?</code>,
<code class="literal">S{1,UINT_MAX}?</code> respectively.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">(?>S)</code>
</p>
</td>
<td>
<p>
Matches the best match for <span class="emphasis"><em>S</em></span>, and only that.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">(?=S), (?<=S)</code>
</p>
</td>
<td>
<p>
Matches only the best match for <span class="emphasis"><em>S</em></span> (this is
only visible if there are capturing parenthesis within <span class="emphasis"><em>S</em></span>).
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">(?!S), (?<!S)</code>
</p>
</td>
<td>
<p>
Considers only whether a match for S exists or not.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal">(?(condition)yes-pattern | no-pattern)</code>
</p>
</td>
<td>
<p>
If condition is true, then only yes-pattern is considered, otherwise
only no-pattern is considered.
</p>
</td>
</tr>
</tbody>
</table></div>
<h4>
<a name="boost_regex.syntax.perl_syntax.h45"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.variations"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.variations">Variations</a>
</h4>
<p>
The <a class="link" href="../ref/syntax_option_type/syntax_option_type_perl.html" title="Options for Perl Regular Expressions">options
<code class="literal">normal</code>, <code class="literal">ECMAScript</code>, <code class="literal">JavaScript</code>
and <code class="literal">JScript</code></a> are all synonyms for <code class="literal">perl</code>.
</p>
<h4>
<a name="boost_regex.syntax.perl_syntax.h46"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.options"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.options">Options</a>
</h4>
<p>
There are a <a class="link" href="../ref/syntax_option_type/syntax_option_type_perl.html" title="Options for Perl Regular Expressions">variety
of flags</a> that may be combined with the <code class="literal">perl</code> option
when constructing the regular expression, in particular note that the <code class="literal">newline_alt</code>
option alters the syntax, while the <code class="literal">collate</code>, <code class="literal">nosubs</code>
and <code class="literal">icase</code> options modify how the case and locale sensitivity
are to be applied.
</p>
<h4>
<a name="boost_regex.syntax.perl_syntax.h47"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.pattern_modifiers"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.pattern_modifiers">Pattern
Modifiers</a>
</h4>
<p>
The perl <code class="literal">smix</code> modifiers can either be applied using a
<code class="literal">(?smix-smix)</code> prefix to the regular expression, or with
one of the <a class="link" href="../ref/syntax_option_type/syntax_option_type_perl.html" title="Options for Perl Regular Expressions">regex-compile
time flags <code class="literal">no_mod_m</code>, <code class="literal">mod_x</code>, <code class="literal">mod_s</code>,
and <code class="literal">no_mod_s</code></a>.
</p>
<h4>
<a name="boost_regex.syntax.perl_syntax.h48"></a>
<span class="phrase"><a name="boost_regex.syntax.perl_syntax.references"></a></span><a class="link" href="perl_syntax.html#boost_regex.syntax.perl_syntax.references">References</a>
</h4>
<p>
<a href="http://perldoc.perl.org/perlre.html" target="_top">Perl 5.8</a>.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 1998-2013 John Maddock<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../syntax.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../syntax.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="basic_extended.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|