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
|
<HTML
><HEAD
><TITLE
>Pattern Syntax</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Regular Expression Functions (Perl-Compatible)"
HREF="ref.pcre.html"><LINK
REL="PREVIOUS"
TITLE="Pattern Modifiers"
HREF="pcre.pattern.modifiers.html"><LINK
REL="NEXT"
TITLE="Regular Expression Functions (POSIX Extended)"
HREF="ref.regex.html"><META
NAME="HTTP_EQUIV"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="pcre.pattern.modifiers.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="ref.regex.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="pcre.pattern.syntax"
>Pattern Syntax</A
></H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN33168"
></A
><P
> (unknown)</P
>Pattern Syntax -- Describes PCRE regex syntax</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN33171"
></A
><H2
>Description</H2
><P
CLASS="literallayout"
><br>
The PCRE library is a set of functions that implement regular<br>
expression pattern matching using the same syntax and semantics<br>
as Perl 5, with just a few differences (see below). The current<br>
implementation corresponds to Perl 5.005.<br>
</P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN33174"
></A
><H2
>Differences From Perl</H2
><P
CLASS="literallayout"
><br>
The differences described here are with respect to Perl<br>
5.005.<br>
<br>
1. By default, a whitespace character is any character that<br>
the C library function isspace() recognizes, though it is<br>
possible to compile PCRE with alternative character type<br>
tables. Normally isspace() matches space, formfeed, newline,<br>
carriage return, horizontal tab, and vertical tab. Perl 5 no<br>
longer includes vertical tab in its set of whitespace char-<br>
acters. The \v escape that was in the Perl documentation for<br>
a long time was never in fact recognized. However, the char-<br>
acter itself was treated as whitespace at least up to 5.002.<br>
In 5.004 and 5.005 it does not match \s.<br>
<br>
2. PCRE does not allow repeat quantifiers on lookahead<br>
assertions. Perl permits them, but they do not mean what you<br>
might think. For example, (?!a){3} does not assert that the<br>
next three characters are not "a". It just asserts that the<br>
next character is not "a" three times.<br>
<br>
3. Capturing subpatterns that occur inside negative looka-<br>
head assertions are counted, but their entries in the<br>
offsets vector are never set. Perl sets its numerical vari-<br>
ables from any such patterns that are matched before the<br>
assertion fails to match something (thereby succeeding), but<br>
only if the negative lookahead assertion contains just one<br>
branch.<br>
<br>
4. Though binary zero characters are supported in the sub-<br>
ject string, they are not allowed in a pattern string<br>
because it is passed as a normal C string, terminated by<br>
zero. The escape sequence "\0" can be used in the pattern to<br>
represent a binary zero.<br>
<br>
5. The following Perl escape sequences are not supported:<br>
\l, \u, \L, \U, \E, \Q. In fact these are implemented by<br>
Perl's general string-handling and are not part of its pat-<br>
tern matching engine.<br>
<br>
6. The Perl \G assertion is not supported as it is not<br>
relevant to single pattern matches.<br>
<br>
7. Fairly obviously, PCRE does not support the (?{code})<br>
construction.<br>
<br>
8. There are at the time of writing some oddities in Perl<br>
5.005_02 concerned with the settings of captured strings<br>
when part of a pattern is repeated. For example, matching<br>
"aba" against the pattern /^(a(b)?)+$/ sets $2 to the value<br>
"b", but matching "aabbaa" against /^(aa(bb)?)+$/ leaves $2<br>
unset. However, if the pattern is changed to<br>
/^(aa(b(b))?)+$/ then $2 (and $3) get set.<br>
<br>
In Perl 5.004 $2 is set in both cases, and that is also true<br>
of PCRE. If in the future Perl changes to a consistent state<br>
that is different, PCRE may change to follow.<br>
<br>
9. Another as yet unresolved discrepancy is that in Perl<br>
5.005_02 the pattern /^(a)?(?(1)a|b)+$/ matches the string<br>
"a", whereas in PCRE it does not. However, in both Perl and<br>
PCRE /^(a)?a/ matched against "a" leaves $1 unset.<br>
<br>
10. PCRE provides some extensions to the Perl regular<br>
expression facilities:<br>
<br>
(a) Although lookbehind assertions must match fixed length<br>
strings, each alternative branch of a lookbehind assertion<br>
can match a different length of string. Perl 5.005 requires<br>
them all to have the same length.<br>
<br>
(b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not<br>
set, the $ meta- character matches only at the very end of<br>
the string.<br>
<br>
(c) If PCRE_EXTRA is set, a backslash followed by a letter<br>
with no special meaning is faulted.<br>
<br>
(d) If PCRE_UNGREEDY is set, the greediness of the repeti-<br>
tion quantifiers is inverted, that is, by default they are<br>
not greedy, but if followed by a question mark they are.<br>
</P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN33177"
></A
><H2
>Regular Expression Details</H2
><P
CLASS="literallayout"
><br>
The syntax and semantics of the regular expressions sup-<br>
ported by PCRE are described below. Regular expressions are<br>
also described in the Perl documentation and in a number of<br>
other books, some of which have copious examples. Jeffrey<br>
Friedl's "Mastering Regular Expressions", published by<br>
O'Reilly (ISBN 1-56592-257-3), covers them in great detail.<br>
The description here is intended as reference documentation.<br>
<br>
A regular expression is a pattern that is matched against a<br>
subject string from left to right. Most characters stand for<br>
themselves in a pattern, and match the corresponding charac-<br>
ters in the subject. As a trivial example, the pattern<br>
<br>
The quick brown fox<br>
<br>
matches a portion of a subject string that is identical to<br>
itself. The power of regular expressions comes from the<br>
ability to include alternatives and repetitions in the pat-<br>
tern. These are encoded in the pattern by the use of <I
CLASS="emphasis"
>meta</I
>-<br>
<I
CLASS="emphasis"
>characters</I
>, which do not stand for themselves but instead<br>
are interpreted in some special way.<br>
<br>
There are two different sets of meta-characters: those that<br>
are recognized anywhere in the pattern except within square<br>
brackets, and those that are recognized in square brackets.<br>
Outside square brackets, the meta-characters are as follows:<br>
<br>
\ general escape character with several uses<br>
^ assert start of subject (or line, in multiline<br>
mode)<br>
$ assert end of subject (or line, in multiline mode)<br>
. match any character except newline (by default)<br>
[ start character class definition<br>
| start of alternative branch<br>
( start subpattern<br>
) end subpattern<br>
? extends the meaning of (<br>
also 0 or 1 quantifier<br>
also quantifier minimizer<br>
* 0 or more quantifier<br>
+ 1 or more quantifier<br>
{ start min/max quantifier<br>
<br>
Part of a pattern that is in square brackets is called a<br>
"character class". In a character class the only meta-<br>
characters are:<br>
<br>
\ general escape character<br>
^ negate the class, but only if the first character<br>
- indicates character range<br>
] terminates the character class<br>
<br>
The following sections describe the use of each of the<br>
meta-characters.<br>
<br>
BACKSLASH<br>
The backslash character has several uses. Firstly, if it is<br>
followed by a non-alphameric character, it takes away any<br>
special meaning that character may have. This use of<br>
backslash as an escape character applies both inside and<br>
outside character classes.<br>
<br>
For example, if you want to match a "*" character, you write<br>
"\*" in the pattern. This applies whether or not the follow-<br>
ing character would otherwise be interpreted as a meta-<br>
character, so it is always safe to precede a non-alphameric<br>
with "\" to specify that it stands for itself. In particu-<br>
lar, if you want to match a backslash, you write "\\".<br>
<br>
If a pattern is compiled with the PCRE_EXTENDED option, whi-<br>
tespace in the pattern (other than in a character class) and<br>
characters between a "#" outside a character class and the<br>
next newline character are ignored. An escaping backslash<br>
can be used to include a whitespace or "#" character as part<br>
of the pattern.<br>
<br>
A second use of backslash provides a way of encoding non-<br>
printing characters in patterns in a visible manner. There<br>
is no restriction on the appearance of non-printing charac-<br>
ters, apart from the binary zero that terminates a pattern,<br>
but when a pattern is being prepared by text editing, it is<br>
usually easier to use one of the following escape sequences<br>
than the binary character it represents:<br>
<br>
\a alarm, that is, the BEL character (hex 07)<br>
\cx "control-x", where x is any character<br>
\e escape (hex 1B)<br>
\f formfeed (hex 0C)<br>
\n newline (hex 0A)<br>
\r carriage return (hex 0D)<br>
\t tab (hex 09)<br>
\xhh character with hex code hh<br>
\ddd character with octal code ddd, or backreference<br>
<br>
The precise effect of "\cx" is as follows: if "x" is a lower<br>
case letter, it is converted to upper case. Then bit 6 of<br>
the character (hex 40) is inverted. Thus "\cz" becomes hex<br>
1A, but "\c{" becomes hex 3B, while "\c;" becomes hex 7B.<br>
<br>
After "\x", up to two hexadecimal digits are read (letters<br>
can be in upper or lower case).<br>
<br>
After "\0" up to two further octal digits are read. In both<br>
cases, if there are fewer than two digits, just those that<br>
are present are used. Thus the sequence "\0\x\07" specifies<br>
two binary zeros followed by a BEL character. Make sure you<br>
supply two digits after the initial zero if the character<br>
that follows is itself an octal digit.<br>
<br>
The handling of a backslash followed by a digit other than 0<br>
is complicated. Outside a character class, PCRE reads it<br>
and any following digits as a decimal number. If the number<br>
is less than 10, or if there have been at least that many<br>
previous capturing left parentheses in the expression, the<br>
entire sequence is taken as a <I
CLASS="emphasis"
>back</I
> <I
CLASS="emphasis"
>reference</I
>. A description<br>
of how this works is given later, following the discussion<br>
of parenthesized subpatterns.<br>
<br>
Inside a character class, or if the decimal number is<br>
greater than 9 and there have not been that many capturing<br>
subpatterns, PCRE re-reads up to three octal digits follow-<br>
ing the backslash, and generates a single byte from the<br>
least significant 8 bits of the value. Any subsequent digits<br>
stand for themselves. For example:<br>
<br>
\040 is another way of writing a space<br>
\40 is the same, provided there are fewer than 40<br>
previous capturing subpatterns<br>
\7 is always a back reference<br>
\11 might be a back reference, or another way of<br>
writing a tab<br>
\011 is always a tab<br>
\0113 is a tab followed by the character "3"<br>
\113 is the character with octal code 113 (since there<br>
can be no more than 99 back references)<br>
\377 is a byte consisting entirely of 1 bits<br>
\81 is either a back reference, or a binary zero<br>
followed by the two characters "8" and "1"<br>
<br>
Note that octal values of 100 or greater must not be intro-<br>
duced by a leading zero, because no more than three octal<br>
digits are ever read.<br>
<br>
All the sequences that define a single byte value can be<br>
used both inside and outside character classes. In addition,<br>
inside a character class, the sequence "\b" is interpreted<br>
as the backspace character (hex 08). Outside a character<br>
class it has a different meaning (see below).<br>
<br>
The third use of backslash is for specifying generic charac-<br>
ter types:<br>
<br>
\d any decimal digit<br>
\D any character that is not a decimal digit<br>
\s any whitespace character<br>
\S any character that is not a whitespace character<br>
\w any "word" character<br>
\W any "non-word" character<br>
<br>
Each pair of escape sequences partitions the complete set of<br>
characters into two disjoint sets. Any given character<br>
matches one, and only one, of each pair.<br>
<br>
A "word" character is any letter or digit or the underscore<br>
character, that is, any character which can be part of a<br>
Perl "word". The definition of letters and digits is con-<br>
trolled by PCRE's character tables, and may vary if locale-<br>
specific matching is taking place (see "Locale support"<br>
above). For example, in the "fr" (French) locale, some char-<br>
acter codes greater than 128 are used for accented letters,<br>
and these are matched by \w.<br>
<br>
These character type sequences can appear both inside and<br>
outside character classes. They each match one character of<br>
the appropriate type. If the current matching point is at<br>
the end of the subject string, all of them fail, since there<br>
is no character to match.<br>
<br>
The fourth use of backslash is for certain simple asser-<br>
tions. An assertion specifies a condition that has to be met<br>
at a particular point in a match, without consuming any<br>
characters from the subject string. The use of subpatterns<br>
for more complicated assertions is described below. The<br>
backslashed assertions are<br>
<br>
\b word boundary<br>
\B not a word boundary<br>
\A start of subject (independent of multiline mode)<br>
\Z end of subject or newline at end (independent of<br>
multiline mode)<br>
\z end of subject (independent of multiline mode)<br>
<br>
These assertions may not appear in character classes (but<br>
note that "\b" has a different meaning, namely the backspace<br>
character, inside a character class).<br>
<br>
A word boundary is a position in the subject string where<br>
the current character and the previous character do not both<br>
match \w or \W (i.e. one matches \w and the other matches<br>
\W), or the start or end of the string if the first or last<br>
character matches \w, respectively.<br>
<br>
The \A, \Z, and \z assertions differ from the traditional<br>
circumflex and dollar (described below) in that they only<br>
ever match at the very start and end of the subject string,<br>
whatever options are set. They are not affected by the<br>
PCRE_NOTBOL or PCRE_NOTEOL options. The difference between<br>
\Z and \z is that \Z matches before a newline that is the<br>
last character of the string as well as at the end of the<br>
string, whereas \z matches only at the end.<br>
<br>
CIRCUMFLEX AND DOLLAR<br>
Outside a character class, in the default matching mode, the<br>
circumflex character is an assertion which is true only if<br>
the current matching point is at the start of the subject<br>
string. Inside a character class, circumflex has an entirely<br>
different meaning (see below).<br>
<br>
Circumflex need not be the first character of the pattern if<br>
a number of alternatives are involved, but it should be the<br>
first thing in each alternative in which it appears if the<br>
pattern is ever to match that branch. If all possible alter-<br>
natives start with a circumflex, that is, if the pattern is<br>
constrained to match only at the start of the subject, it is<br>
said to be an "anchored" pattern. (There are also other con-<br>
structs that can cause a pattern to be anchored.)<br>
<br>
A dollar character is an assertion which is true only if the<br>
current matching point is at the end of the subject string,<br>
or immediately before a newline character that is the last<br>
character in the string (by default). Dollar need not be the<br>
last character of the pattern if a number of alternatives<br>
are involved, but it should be the last item in any branch<br>
in which it appears. Dollar has no special meaning in a<br>
character class.<br>
<br>
The meaning of dollar can be changed so that it matches only<br>
at the very end of the string, by setting the<br>
PCRE_DOLLAR_ENDONLY option at compile or matching time. This<br>
does not affect the \Z assertion.<br>
<br>
The meanings of the circumflex and dollar characters are<br>
changed if the PCRE_MULTILINE option is set. When this is<br>
the case, they match immediately after and immediately<br>
before an internal "\n" character, respectively, in addition<br>
to matching at the start and end of the subject string. For<br>
example, the pattern /^abc$/ matches the subject string<br>
"def\nabc" in multiline mode, but not otherwise. Conse-<br>
quently, patterns that are anchored in single line mode<br>
because all branches start with "^" are not anchored in mul-<br>
tiline mode. The PCRE_DOLLAR_ENDONLY option is ignored if<br>
PCRE_MULTILINE is set.<br>
<br>
Note that the sequences \A, \Z, and \z can be used to match<br>
the start and end of the subject in both modes, and if all<br>
branches of a pattern start with \A is it always anchored,<br>
whether PCRE_MULTILINE is set or not.<br>
<br>
<br>
<br>
FULL STOP (PERIOD, DOT)<br>
Outside a character class, a dot in the pattern matches any<br>
one character in the subject, including a non-printing<br>
character, but not (by default) newline. If the PCRE_DOTALL<br>
option is set, then dots match newlines as well. The han-<br>
dling of dot is entirely independent of the handling of cir-<br>
cumflex and dollar, the only relationship being that they<br>
both involve newline characters. Dot has no special meaning<br>
in a character class.<br>
<br>
<br>
<br>
SQUARE BRACKETS<br>
An opening square bracket introduces a character class, ter-<br>
minated by a closing square bracket. A closing square<br>
bracket on its own is not special. If a closing square<br>
bracket is required as a member of the class, it should be<br>
the first data character in the class (after an initial cir-<br>
cumflex, if present) or escaped with a backslash.<br>
<br>
A character class matches a single character in the subject;<br>
the character must be in the set of characters defined by<br>
the class, unless the first character in the class is a cir-<br>
cumflex, in which case the subject character must not be in<br>
the set defined by the class. If a circumflex is actually<br>
required as a member of the class, ensure it is not the<br>
first character, or escape it with a backslash.<br>
<br>
For example, the character class [aeiou] matches any lower<br>
case vowel, while [^aeiou] matches any character that is not<br>
a lower case vowel. Note that a circumflex is just a con-<br>
venient notation for specifying the characters which are in<br>
the class by enumerating those that are not. It is not an<br>
assertion: it still consumes a character from the subject<br>
string, and fails if the current pointer is at the end of<br>
the string.<br>
<br>
When caseless matching is set, any letters in a class<br>
represent both their upper case and lower case versions, so<br>
for example, a caseless [aeiou] matches "A" as well as "a",<br>
and a caseless [^aeiou] does not match "A", whereas a case-<br>
ful version would.<br>
<br>
The newline character is never treated in any special way in<br>
character classes, whatever the setting of the PCRE_DOTALL<br>
or PCRE_MULTILINE options is. A class such as [^a] will<br>
always match a newline.<br>
<br>
The minus (hyphen) character can be used to specify a range<br>
of characters in a character class. For example, [d-m]<br>
matches any letter between d and m, inclusive. If a minus<br>
character is required in a class, it must be escaped with a<br>
backslash or appear in a position where it cannot be inter-<br>
preted as indicating a range, typically as the first or last<br>
character in the class.<br>
It is not possible to have the literal character "]" as the<br>
end character of a range. A pattern such as [W-]46] is<br>
interpreted as a class of two characters ("W" and "-") fol-<br>
lowed by a literal string "46]", so it would match "W46]" or<br>
"-46]". However, if the "]" is escaped with a backslash it<br>
is interpreted as the end of range, so [W-\]46] is inter-<br>
preted as a single class containing a range followed by two<br>
separate characters. The octal or hexadecimal representation<br>
of "]" can also be used to end a range.<br>
<br>
Ranges operate in ASCII collating sequence. They can also be<br>
used for characters specified numerically, for example<br>
[\000-\037]. If a range that includes letters is used when<br>
caseless matching is set, it matches the letters in either<br>
case. For example, [W-c] is equivalent to [][\^_`wxyzabc],<br>
matched caselessly, and if character tables for the "fr"<br>
locale are in use, [\xc8-\xcb] matches accented E characters<br>
in both cases.<br>
<br>
The character types \d, \D, \s, \S, \w, and \W may also<br>
appear in a character class, and add the characters that<br>
they match to the class. For example, [\dABCDEF] matches any<br>
hexadecimal digit. A circumflex can conveniently be used<br>
with the upper case character types to specify a more res-<br>
tricted set of characters than the matching lower case type.<br>
For example, the class [^\W_] matches any letter or digit,<br>
but not underscore.<br>
<br>
All non-alphameric characters other than \, -, ^ (at the<br>
start) and the terminating ] are non-special in character<br>
classes, but it does no harm if they are escaped.<br>
<br>
<br>
<br>
VERTICAL BAR<br>
Vertical bar characters are used to separate alternative<br>
patterns. For example, the pattern<br>
<br>
gilbert|sullivan<br>
<br>
matches either "gilbert" or "sullivan". Any number of alter-<br>
natives may appear, and an empty alternative is permitted<br>
(matching the empty string). The matching process tries<br>
each alternative in turn, from left to right, and the first<br>
one that succeeds is used. If the alternatives are within a<br>
subpattern (defined below), "succeeds" means matching the<br>
rest of the main pattern as well as the alternative in the<br>
subpattern.<br>
<br>
<br>
<br>
<br>
INTERNAL OPTION SETTING<br>
The settings of PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL,<br>
and PCRE_EXTENDED can be changed from within the pattern by<br>
a sequence of Perl option letters enclosed between "(?" and<br>
")". The option letters are<br>
<br>
i for PCRE_CASELESS<br>
m for PCRE_MULTILINE<br>
s for PCRE_DOTALL<br>
x for PCRE_EXTENDED<br>
<br>
For example, (?im) sets caseless, multiline matching. It is<br>
also possible to unset these options by preceding the letter<br>
with a hyphen, and a combined setting and unsetting such as<br>
(?im-sx), which sets PCRE_CASELESS and PCRE_MULTILINE while<br>
unsetting PCRE_DOTALL and PCRE_EXTENDED, is also permitted.<br>
If a letter appears both before and after the hyphen, the<br>
option is unset.<br>
<br>
The scope of these option changes depends on where in the<br>
pattern the setting occurs. For settings that are outside<br>
any subpattern (defined below), the effect is the same as if<br>
the options were set or unset at the start of matching. The<br>
following patterns all behave in exactly the same way:<br>
<br>
(?i)abc<br>
a(?i)bc<br>
ab(?i)c<br>
abc(?i)<br>
<br>
which in turn is the same as compiling the pattern abc with<br>
PCRE_CASELESS set. In other words, such "top level" set-<br>
tings apply to the whole pattern (unless there are other<br>
changes inside subpatterns). If there is more than one set-<br>
ting of the same option at top level, the rightmost setting<br>
is used.<br>
<br>
If an option change occurs inside a subpattern, the effect<br>
is different. This is a change of behaviour in Perl 5.005.<br>
An option change inside a subpattern affects only that part<br>
of the subpattern that follows it, so<br>
<br>
(a(?i)b)c<br>
<br>
matches abc and aBc and no other strings (assuming<br>
PCRE_CASELESS is not used). By this means, options can be<br>
made to have different settings in different parts of the<br>
pattern. Any changes made in one alternative do carry on<br>
into subsequent branches within the same subpattern. For<br>
example,<br>
<br>
(a(?i)b|c)<br>
<br>
matches "ab", "aB", "c", and "C", even though when matching<br>
"C" the first branch is abandoned before the option setting.<br>
This is because the effects of option settings happen at<br>
compile time. There would be some very weird behaviour oth-<br>
erwise.<br>
<br>
The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can<br>
be changed in the same way as the Perl-compatible options by<br>
using the characters U and X respectively. The (?X) flag<br>
setting is special in that it must always occur earlier in<br>
the pattern than any of the additional features it turns on,<br>
even when it is at top level. It is best put at the start.<br>
<br>
<br>
<br>
SUBPATTERNS<br>
Subpatterns are delimited by parentheses (round brackets),<br>
which can be nested. Marking part of a pattern as a subpat-<br>
tern does two things:<br>
<br>
1. It localizes a set of alternatives. For example, the pat-<br>
tern<br>
<br>
cat(aract|erpillar|)<br>
<br>
matches one of the words "cat", "cataract", or "caterpil-<br>
lar". Without the parentheses, it would match "cataract",<br>
"erpillar" or the empty string.<br>
<br>
2. It sets up the subpattern as a capturing subpattern (as<br>
defined above). When the whole pattern matches, that por-<br>
tion of the subject string that matched the subpattern is<br>
passed back to the caller via the <I
CLASS="emphasis"
>ovector</I
> argument of<br>
<B
CLASS="function"
>pcre_exec()</B
>. Opening parentheses are counted from left to<br>
right (starting from 1) to obtain the numbers of the captur-<br>
ing subpatterns.<br>
<br>
For example, if the string "the red king" is matched against<br>
the pattern<br>
<br>
the ((red|white) (king|queen))<br>
<br>
the captured substrings are "red king", "red", and "king",<br>
and are numbered 1, 2, and 3.<br>
<br>
The fact that plain parentheses fulfil two functions is not<br>
always helpful. There are often times when a grouping sub-<br>
pattern is required without a capturing requirement. If an<br>
opening parenthesis is followed by "?:", the subpattern does<br>
not do any capturing, and is not counted when computing the<br>
number of any subsequent capturing subpatterns. For example,<br>
if the string "the white queen" is matched against the<br>
pattern<br>
<br>
the ((?:red|white) (king|queen))<br>
<br>
the captured substrings are "white queen" and "queen", and<br>
are numbered 1 and 2. The maximum number of captured sub-<br>
strings is 99, and the maximum number of all subpatterns,<br>
both capturing and non-capturing, is 200.<br>
<br>
As a convenient shorthand, if any option settings are<br>
required at the start of a non-capturing subpattern, the<br>
option letters may appear between the "?" and the ":". Thus<br>
the two patterns<br>
<br>
(?i:saturday|sunday)<br>
(?:(?i)saturday|sunday)<br>
<br>
match exactly the same set of strings. Because alternative<br>
branches are tried from left to right, and options are not<br>
reset until the end of the subpattern is reached, an option<br>
setting in one branch does affect subsequent branches, so<br>
the above patterns match "SUNDAY" as well as "Saturday".<br>
<br>
<br>
<br>
REPETITION<br>
Repetition is specified by quantifiers, which can follow any<br>
of the following items:<br>
<br>
a single character, possibly escaped<br>
the . metacharacter<br>
a character class<br>
a back reference (see next section)<br>
a parenthesized subpattern (unless it is an assertion -<br>
see below)<br>
<br>
The general repetition quantifier specifies a minimum and<br>
maximum number of permitted matches, by giving the two<br>
numbers in curly brackets (braces), separated by a comma.<br>
The numbers must be less than 65536, and the first must be<br>
less than or equal to the second. For example:<br>
<br>
z{2,4}<br>
<br>
matches "zz", "zzz", or "zzzz". A closing brace on its own<br>
is not a special character. If the second number is omitted,<br>
but the comma is present, there is no upper limit; if the<br>
second number and the comma are both omitted, the quantifier<br>
specifies an exact number of required matches. Thus<br>
<br>
[aeiou]{3,}<br>
<br>
matches at least 3 successive vowels, but may match many<br>
more, while<br>
<br>
\d{8}<br>
<br>
matches exactly 8 digits. An opening curly bracket that<br>
appears in a position where a quantifier is not allowed, or<br>
one that does not match the syntax of a quantifier, is taken<br>
as a literal character. For example, {,6} is not a quantif-<br>
ier, but a literal string of four characters.<br>
<br>
The quantifier {0} is permitted, causing the expression to<br>
behave as if the previous item and the quantifier were not<br>
present.<br>
<br>
For convenience (and historical compatibility) the three<br>
most common quantifiers have single-character abbreviations:<br>
<br>
* is equivalent to {0,}<br>
+ is equivalent to {1,}<br>
? is equivalent to {0,1}<br>
<br>
It is possible to construct infinite loops by following a<br>
subpattern that can match no characters with a quantifier<br>
that has no upper limit, for example:<br>
<br>
(a?)*<br>
<br>
Earlier versions of Perl and PCRE used to give an error at<br>
compile time for such patterns. However, because there are<br>
cases where this can be useful, such patterns are now<br>
accepted, but if any repetition of the subpattern does in<br>
fact match no characters, the loop is forcibly broken.<br>
<br>
By default, the quantifiers are "greedy", that is, they<br>
match as much as possible (up to the maximum number of per-<br>
mitted times), without causing the rest of the pattern to<br>
fail. The classic example of where this gives problems is in<br>
trying to match comments in C programs. These appear between<br>
the sequences /* and */ and within the sequence, individual<br>
* and / characters may appear. An attempt to match C com-<br>
ments by applying the pattern<br>
<br>
/\*.*\*/<br>
<br>
to the string<br>
<br>
/* first command */ not comment /* second comment */<br>
<br>
fails, because it matches the entire string due to the<br>
greediness of the .* item.<br>
<br>
However, if a quantifier is followed by a question mark,<br>
then it ceases to be greedy, and instead matches the minimum<br>
number of times possible, so the pattern<br>
<br>
/\*.*?\*/<br>
<br>
does the right thing with the C comments. The meaning of the<br>
various quantifiers is not otherwise changed, just the pre-<br>
ferred number of matches. Do not confuse this use of ques-<br>
tion mark with its use as a quantifier in its own right.<br>
Because it has two uses, it can sometimes appear doubled, as<br>
in<br>
<br>
\d??\d<br>
<br>
which matches one digit by preference, but can match two if<br>
that is the only way the rest of the pattern matches.<br>
<br>
If the PCRE_UNGREEDY option is set (an option which is not<br>
available in Perl) then the quantifiers are not greedy by<br>
default, but individual ones can be made greedy by following<br>
them with a question mark. In other words, it inverts the<br>
default behaviour.<br>
<br>
When a parenthesized subpattern is quantified with a minimum<br>
repeat count that is greater than 1 or with a limited max-<br>
imum, more store is required for the compiled pattern, in<br>
proportion to the size of the minimum or maximum.<br>
<br>
If a pattern starts with .* or .{0,} and the PCRE_DOTALL<br>
option (equivalent to Perl's /s) is set, thus allowing the .<br>
to match newlines, then the pattern is implicitly anchored,<br>
because whatever follows will be tried against every charac-<br>
ter position in the subject string, so there is no point in<br>
retrying the overall match at any position after the first.<br>
PCRE treats such a pattern as though it were preceded by \A.<br>
In cases where it is known that the subject string contains<br>
no newlines, it is worth setting PCRE_DOTALL when the pat-<br>
tern begins with .* in order to obtain this optimization, or<br>
alternatively using ^ to indicate anchoring explicitly.<br>
<br>
When a capturing subpattern is repeated, the value captured<br>
is the substring that matched the final iteration. For exam-<br>
ple, after<br>
<br>
(tweedle[dume]{3}\s*)+<br>
<br>
has matched "tweedledum tweedledee" the value of the cap-<br>
tured substring is "tweedledee". However, if there are<br>
nested capturing subpatterns, the corresponding captured<br>
values may have been set in previous iterations. For exam-<br>
ple, after<br>
/(a|(b))+/<br>
<br>
matches "aba" the value of the second captured substring is<br>
"b".<br>
<br>
<br>
<br>
BACK REFERENCES<br>
Outside a character class, a backslash followed by a digit<br>
greater than 0 (and possibly further digits) is a back<br>
reference to a capturing subpattern earlier (i.e. to its<br>
left) in the pattern, provided there have been that many<br>
previous capturing left parentheses.<br>
<br>
However, if the decimal number following the backslash is<br>
less than 10, it is always taken as a back reference, and<br>
causes an error only if there are not that many capturing<br>
left parentheses in the entire pattern. In other words, the<br>
parentheses that are referenced need not be to the left of<br>
the reference for numbers less than 10. See the section<br>
entitled "Backslash" above for further details of the han-<br>
dling of digits following a backslash.<br>
<br>
A back reference matches whatever actually matched the cap-<br>
turing subpattern in the current subject string, rather than<br>
anything matching the subpattern itself. So the pattern<br>
<br>
(sens|respons)e and \1ibility<br>
<br>
matches "sense and sensibility" and "response and responsi-<br>
bility", but not "sense and responsibility". If caseful<br>
matching is in force at the time of the back reference, then<br>
the case of letters is relevant. For example,<br>
<br>
((?i)rah)\s+\1<br>
<br>
matches "rah rah" and "RAH RAH", but not "RAH rah", even<br>
though the original capturing subpattern is matched case-<br>
lessly.<br>
<br>
There may be more than one back reference to the same sub-<br>
pattern. If a subpattern has not actually been used in a<br>
particular match, then any back references to it always<br>
fail. For example, the pattern<br>
<br>
(a|(bc))\2<br>
<br>
always fails if it starts to match "a" rather than "bc".<br>
Because there may be up to 99 back references, all digits<br>
following the backslash are taken as part of a potential<br>
back reference number. If the pattern continues with a digit<br>
character, then some delimiter must be used to terminate the<br>
back reference. If the PCRE_EXTENDED option is set, this can<br>
be whitespace. Otherwise an empty comment can be used.<br>
<br>
A back reference that occurs inside the parentheses to which<br>
it refers fails when the subpattern is first used, so, for<br>
example, (a\1) never matches. However, such references can<br>
be useful inside repeated subpatterns. For example, the pat-<br>
tern<br>
<br>
(a|b\1)+<br>
<br>
matches any number of "a"s and also "aba", "ababaa" etc. At<br>
each iteration of the subpattern, the back reference matches<br>
the character string corresponding to the previous itera-<br>
tion. In order for this to work, the pattern must be such<br>
that the first iteration does not need to match the back<br>
reference. This can be done using alternation, as in the<br>
example above, or by a quantifier with a minimum of zero.<br>
<br>
<br>
<br>
ASSERTIONS<br>
An assertion is a test on the characters following or<br>
preceding the current matching point that does not actually<br>
consume any characters. The simple assertions coded as \b,<br>
\B, \A, \Z, \z, ^ and $ are described above. More compli-<br>
cated assertions are coded as subpatterns. There are two<br>
kinds: those that look ahead of the current position in the<br>
subject string, and those that look behind it.<br>
<br>
An assertion subpattern is matched in the normal way, except<br>
that it does not cause the current matching position to be<br>
changed. Lookahead assertions start with (?= for positive<br>
assertions and (?! for negative assertions. For example,<br>
<br>
\w+(?=;)<br>
<br>
matches a word followed by a semicolon, but does not include<br>
the semicolon in the match, and<br>
<br>
foo(?!bar)<br>
<br>
matches any occurrence of "foo" that is not followed by<br>
"bar". Note that the apparently similar pattern<br>
<br>
(?!foo)bar<br>
<br>
does not find an occurrence of "bar" that is preceded by<br>
something other than "foo"; it finds any occurrence of "bar"<br>
whatsoever, because the assertion (?!foo) is always true<br>
when the next three characters are "bar". A lookbehind<br>
assertion is needed to achieve this effect.<br>
Lookbehind assertions start with (?<= for positive asser-<br>
tions and (?<! for negative assertions. For example,<br>
<br>
(?<!foo)bar<br>
<br>
does find an occurrence of "bar" that is not preceded by<br>
"foo". The contents of a lookbehind assertion are restricted<br>
such that all the strings it matches must have a fixed<br>
length. However, if there are several alternatives, they do<br>
not all have to have the same fixed length. Thus<br>
<br>
(?<=bullock|donkey)<br>
<br>
is permitted, but<br>
<br>
(?<!dogs?|cats?)<br>
<br>
causes an error at compile time. Branches that match dif-<br>
ferent length strings are permitted only at the top level of<br>
a lookbehind assertion. This is an extension compared with<br>
Perl 5.005, which requires all branches to match the same<br>
length of string. An assertion such as<br>
<br>
(?<=ab(c|de))<br>
<br>
is not permitted, because its single top-level branch can<br>
match two different lengths, but it is acceptable if rewrit-<br>
ten to use two top-level branches:<br>
<br>
(?<=abc|abde)<br>
<br>
The implementation of lookbehind assertions is, for each<br>
alternative, to temporarily move the current position back<br>
by the fixed width and then try to match. If there are<br>
insufficient characters before the current position, the<br>
match is deemed to fail. Lookbehinds in conjunction with<br>
once-only subpatterns can be particularly useful for match-<br>
ing at the ends of strings; an example is given at the end<br>
of the section on once-only subpatterns.<br>
<br>
Several assertions (of any sort) may occur in succession.<br>
For example,<br>
<br>
(?<=\d{3})(?<!999)foo<br>
<br>
matches "foo" preceded by three digits that are not "999".<br>
Furthermore, assertions can be nested in any combination.<br>
For example,<br>
<br>
(?<=(?<!foo)bar)baz<br>
<br>
matches an occurrence of "baz" that is preceded by "bar"<br>
which in turn is not preceded by "foo".<br>
<br>
Assertion subpatterns are not capturing subpatterns, and may<br>
not be repeated, because it makes no sense to assert the<br>
same thing several times. If an assertion contains capturing<br>
subpatterns within it, these are always counted for the pur-<br>
poses of numbering the capturing subpatterns in the whole<br>
pattern. Substring capturing is carried out for positive<br>
assertions, but it does not make sense for negative asser-<br>
tions.<br>
<br>
Assertions count towards the maximum of 200 parenthesized<br>
subpatterns.<br>
<br>
<br>
<br>
ONCE-ONLY SUBPATTERNS<br>
With both maximizing and minimizing repetition, failure of<br>
what follows normally causes the repeated item to be re-<br>
evaluated to see if a different number of repeats allows the<br>
rest of the pattern to match. Sometimes it is useful to<br>
prevent this, either to change the nature of the match, or<br>
to cause it fail earlier than it otherwise might, when the<br>
author of the pattern knows there is no point in carrying<br>
on.<br>
<br>
Consider, for example, the pattern \d+foo when applied to<br>
the subject line<br>
<br>
123456bar<br>
<br>
After matching all 6 digits and then failing to match "foo",<br>
the normal action of the matcher is to try again with only 5<br>
digits matching the \d+ item, and then with 4, and so on,<br>
before ultimately failing. Once-only subpatterns provide the<br>
means for specifying that once a portion of the pattern has<br>
matched, it is not to be re-evaluated in this way, so the<br>
matcher would give up immediately on failing to match "foo"<br>
the first time. The notation is another kind of special<br>
parenthesis, starting with (?> as in this example:<br>
<br>
(?>\d+)bar<br>
<br>
This kind of parenthesis "locks up" the part of the pattern<br>
it contains once it has matched, and a failure further into<br>
the pattern is prevented from backtracking into it. Back-<br>
tracking past it to previous items, however, works as nor-<br>
mal.<br>
<br>
An alternative description is that a subpattern of this type<br>
matches the string of characters that an identical stan-<br>
dalone pattern would match, if anchored at the current point<br>
in the subject string.<br>
<br>
Once-only subpatterns are not capturing subpatterns. Simple<br>
cases such as the above example can be thought of as a max-<br>
imizing repeat that must swallow everything it can. So,<br>
while both \d+ and \d+? are prepared to adjust the number of<br>
digits they match in order to make the rest of the pattern<br>
match, (?>\d+) can only match an entire sequence of digits.<br>
<br>
This construction can of course contain arbitrarily compli-<br>
cated subpatterns, and it can be nested.<br>
<br>
Once-only subpatterns can be used in conjunction with look-<br>
behind assertions to specify efficient matching at the end<br>
of the subject string. Consider a simple pattern such as<br>
<br>
abcd$<br>
<br>
when applied to a long string which does not match it.<br>
Because matching proceeds from left to right, PCRE will look<br>
for each "a" in the subject and then see if what follows<br>
matches the rest of the pattern. If the pattern is specified<br>
as<br>
<br>
^.*abcd$<br>
<br>
then the initial .* matches the entire string at first, but<br>
when this fails, it backtracks to match all but the last<br>
character, then all but the last two characters, and so on.<br>
Once again the search for "a" covers the entire string, from<br>
right to left, so we are no better off. However, if the pat-<br>
tern is written as<br>
<br>
^(?>.*)(?<=abcd)<br>
<br>
then there can be no backtracking for the .* item; it can<br>
match only the entire string. The subsequent lookbehind<br>
assertion does a single test on the last four characters. If<br>
it fails, the match fails immediately. For long strings,<br>
this approach makes a significant difference to the process-<br>
ing time.<br>
<br>
<br>
<br>
CONDITIONAL SUBPATTERNS<br>
It is possible to cause the matching process to obey a sub-<br>
pattern conditionally or to choose between two alternative<br>
subpatterns, depending on the result of an assertion, or<br>
whether a previous capturing subpattern matched or not. The<br>
two possible forms of conditional subpattern are<br>
<br>
(?(condition)yes-pattern)<br>
(?(condition)yes-pattern|no-pattern)<br>
<br>
If the condition is satisfied, the yes-pattern is used; oth-<br>
erwise the no-pattern (if present) is used. If there are<br>
more than two alternatives in the subpattern, a compile-time<br>
error occurs.<br>
<br>
There are two kinds of condition. If the text between the<br>
parentheses consists of a sequence of digits, then the con-<br>
dition is satisfied if the capturing subpattern of that<br>
number has previously matched. Consider the following pat-<br>
tern, which contains non-significant white space to make it<br>
more readable (assume the PCRE_EXTENDED option) and to<br>
divide it into three parts for ease of discussion:<br>
<br>
( \( )? [^()]+ (?(1) \) )<br>
<br>
The first part matches an optional opening parenthesis, and<br>
if that character is present, sets it as the first captured<br>
substring. The second part matches one or more characters<br>
that are not parentheses. The third part is a conditional<br>
subpattern that tests whether the first set of parentheses<br>
matched or not. If they did, that is, if subject started<br>
with an opening parenthesis, the condition is true, and so<br>
the yes-pattern is executed and a closing parenthesis is<br>
required. Otherwise, since no-pattern is not present, the<br>
subpattern matches nothing. In other words, this pattern<br>
matches a sequence of non-parentheses, optionally enclosed<br>
in parentheses.<br>
<br>
If the condition is not a sequence of digits, it must be an<br>
assertion. This may be a positive or negative lookahead or<br>
lookbehind assertion. Consider this pattern, again contain-<br>
ing non-significant white space, and with the two alterna-<br>
tives on the second line:<br>
<br>
(?(?=[^a-z]*[a-z])<br>
\d{2}[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )<br>
<br>
The condition is a positive lookahead assertion that matches<br>
an optional sequence of non-letters followed by a letter. In<br>
other words, it tests for the presence of at least one<br>
letter in the subject. If a letter is found, the subject is<br>
matched against the first alternative; otherwise it is<br>
matched against the second. This pattern matches strings in<br>
one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are<br>
letters and dd are digits.<br>
<br>
<br>
<br>
COMMENTS<br>
The sequence (?# marks the start of a comment which<br>
continues up to the next closing parenthesis. Nested<br>
parentheses are not permitted. The characters that make up a<br>
comment play no part in the pattern matching at all.<br>
<br>
If the PCRE_EXTENDED option is set, an unescaped # character<br>
outside a character class introduces a comment that contin-<br>
ues up to the next newline character in the pattern.<br>
<br>
<br>
<br>
PERFORMANCE<br>
Certain items that may appear in patterns are more efficient<br>
than others. It is more efficient to use a character class<br>
like [aeiou] than a set of alternatives such as (a|e|i|o|u).<br>
In general, the simplest construction that provides the<br>
required behaviour is usually the most efficient. Jeffrey<br>
Friedl's book contains a lot of discussion about optimizing<br>
regular expressions for efficient performance.<br>
<br>
When a pattern begins with .* and the PCRE_DOTALL option is<br>
set, the pattern is implicitly anchored by PCRE, since it<br>
can match only at the start of a subject string. However, if<br>
PCRE_DOTALL is not set, PCRE cannot make this optimization,<br>
because the . metacharacter does not then match a newline,<br>
and if the subject string contains newlines, the pattern may<br>
match from the character immediately following one of them<br>
instead of from the very start. For example, the pattern<br>
<br>
(.*) second<br>
<br>
matches the subject "first\nand second" (where \n stands for<br>
a newline character) with the first captured substring being<br>
"and". In order to do this, PCRE has to retry the match<br>
starting after every newline in the subject.<br>
<br>
If you are using such a pattern with subject strings that do<br>
not contain newlines, the best performance is obtained by<br>
setting PCRE_DOTALL, or starting the pattern with ^.* to<br>
indicate explicit anchoring. That saves PCRE from having to<br>
scan along the subject looking for a newline to restart at.<br>
</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="pcre.pattern.modifiers.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="ref.regex.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Pattern Modifiers</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.pcre.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Regular Expression Functions (POSIX Extended)</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|