30 |
PCRE when its main matching function, \fBpcre_exec()\fP, is used. |
PCRE when its main matching function, \fBpcre_exec()\fP, is used. |
31 |
From release 6.0, PCRE offers a second matching function, |
From release 6.0, PCRE offers a second matching function, |
32 |
\fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not |
\fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not |
33 |
Perl-compatible. The advantages and disadvantages of the alternative function, |
Perl-compatible. Some of the features discussed below are not available when |
34 |
and how it differs from the normal function, are discussed in the |
\fBpcre_dfa_exec()\fP is used. The advantages and disadvantages of the |
35 |
|
alternative function, and how it differs from the normal function, are |
36 |
|
discussed in the |
37 |
.\" HREF |
.\" HREF |
38 |
\fBpcrematching\fP |
\fBpcrematching\fP |
39 |
.\" |
.\" |
241 |
.rs |
.rs |
242 |
.sp |
.sp |
243 |
The sequence \eg followed by a positive or negative number, optionally enclosed |
The sequence \eg followed by a positive or negative number, optionally enclosed |
244 |
in braces, is an absolute or relative back reference. Back references are |
in braces, is an absolute or relative back reference. A named back reference |
245 |
discussed |
can be coded as \eg{name}. Back references are discussed |
246 |
.\" HTML <a href="#backreferences"> |
.\" HTML <a href="#backreferences"> |
247 |
.\" </a> |
.\" </a> |
248 |
later, |
later, |
262 |
.sp |
.sp |
263 |
\ed any decimal digit |
\ed any decimal digit |
264 |
\eD any character that is not a decimal digit |
\eD any character that is not a decimal digit |
265 |
|
\eh any horizontal whitespace character |
266 |
|
\eH any character that is not a horizontal whitespace character |
267 |
\es any whitespace character |
\es any whitespace character |
268 |
\eS any character that is not a whitespace character |
\eS any character that is not a whitespace character |
269 |
|
\ev any vertical whitespace character |
270 |
|
\eV any character that is not a vertical whitespace character |
271 |
\ew any "word" character |
\ew any "word" character |
272 |
\eW any "non-word" character |
\eW any "non-word" character |
273 |
.sp |
.sp |
281 |
.P |
.P |
282 |
For compatibility with Perl, \es does not match the VT character (code 11). |
For compatibility with Perl, \es does not match the VT character (code 11). |
283 |
This makes it different from the the POSIX "space" class. The \es characters |
This makes it different from the the POSIX "space" class. The \es characters |
284 |
are HT (9), LF (10), FF (12), CR (13), and space (32). (If "use locale;" is |
are HT (9), LF (10), FF (12), CR (13), and space (32). If "use locale;" is |
285 |
included in a Perl script, \es may match the VT character. In PCRE, it never |
included in a Perl script, \es may match the VT character. In PCRE, it never |
286 |
does.) |
does. |
287 |
|
.P |
288 |
|
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
289 |
|
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
290 |
|
character property support is available. These sequences retain their original |
291 |
|
meanings from before UTF-8 support was available, mainly for efficiency |
292 |
|
reasons. |
293 |
|
.P |
294 |
|
The sequences \eh, \eH, \ev, and \eV are Perl 5.10 features. In contrast to the |
295 |
|
other sequences, these do match certain high-valued codepoints in UTF-8 mode. |
296 |
|
The horizontal space characters are: |
297 |
|
.sp |
298 |
|
U+0009 Horizontal tab |
299 |
|
U+0020 Space |
300 |
|
U+00A0 Non-break space |
301 |
|
U+1680 Ogham space mark |
302 |
|
U+180E Mongolian vowel separator |
303 |
|
U+2000 En quad |
304 |
|
U+2001 Em quad |
305 |
|
U+2002 En space |
306 |
|
U+2003 Em space |
307 |
|
U+2004 Three-per-em space |
308 |
|
U+2005 Four-per-em space |
309 |
|
U+2006 Six-per-em space |
310 |
|
U+2007 Figure space |
311 |
|
U+2008 Punctuation space |
312 |
|
U+2009 Thin space |
313 |
|
U+200A Hair space |
314 |
|
U+202F Narrow no-break space |
315 |
|
U+205F Medium mathematical space |
316 |
|
U+3000 Ideographic space |
317 |
|
.sp |
318 |
|
The vertical space characters are: |
319 |
|
.sp |
320 |
|
U+000A Linefeed |
321 |
|
U+000B Vertical tab |
322 |
|
U+000C Formfeed |
323 |
|
U+000D Carriage return |
324 |
|
U+0085 Next line |
325 |
|
U+2028 Line separator |
326 |
|
U+2029 Paragraph separator |
327 |
.P |
.P |
328 |
A "word" character is an underscore or any character less than 256 that is a |
A "word" character is an underscore or any character less than 256 that is a |
329 |
letter or digit. The definition of letters and digits is controlled by PCRE's |
letter or digit. The definition of letters and digits is controlled by PCRE's |
337 |
.\" HREF |
.\" HREF |
338 |
\fBpcreapi\fP |
\fBpcreapi\fP |
339 |
.\" |
.\" |
340 |
page). For example, in the "fr_FR" (French) locale, some character codes |
page). For example, in a French locale such as "fr_FR" in Unix-like systems, |
341 |
greater than 128 are used for accented letters, and these are matched by \ew. |
or "french" in Windows, some character codes greater than 128 are used for |
342 |
.P |
accented letters, and these are matched by \ew. The use of locales with Unicode |
343 |
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
is discouraged. |
|
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
|
|
character property support is available. The use of locales with Unicode is |
|
|
discouraged. |
|
344 |
. |
. |
345 |
. |
. |
346 |
.SS "Newline sequences" |
.SS "Newline sequences" |
347 |
.rs |
.rs |
348 |
.sp |
.sp |
349 |
Outside a character class, the escape sequence \eR matches any Unicode newline |
Outside a character class, the escape sequence \eR matches any Unicode newline |
350 |
sequence. This is an extension to Perl. In non-UTF-8 mode \eR is equivalent to |
sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \eR is equivalent to |
351 |
the following: |
the following: |
352 |
.sp |
.sp |
353 |
(?>\er\en|\en|\ex0b|\ef|\er|\ex85) |
(?>\er\en|\en|\ex0b|\ef|\er|\ex85) |
561 |
properties in PCRE. |
properties in PCRE. |
562 |
. |
. |
563 |
. |
. |
564 |
|
.\" HTML <a name="resetmatchstart"></a> |
565 |
|
.SS "Resetting the match start" |
566 |
|
.rs |
567 |
|
.sp |
568 |
|
The escape sequence \eK, which is a Perl 5.10 feature, causes any previously |
569 |
|
matched characters not to be included in the final matched sequence. For |
570 |
|
example, the pattern: |
571 |
|
.sp |
572 |
|
foo\eKbar |
573 |
|
.sp |
574 |
|
matches "foobar", but reports that it has matched "bar". This feature is |
575 |
|
similar to a lookbehind assertion |
576 |
|
.\" HTML <a href="#lookbehind"> |
577 |
|
.\" </a> |
578 |
|
(described below). |
579 |
|
.\" |
580 |
|
However, in this case, the part of the subject before the real match does not |
581 |
|
have to be of fixed length, as lookbehind assertions do. The use of \eK does |
582 |
|
not interfere with the setting of |
583 |
|
.\" HTML <a href="#subpattern"> |
584 |
|
.\" </a> |
585 |
|
captured substrings. |
586 |
|
.\" |
587 |
|
For example, when the pattern |
588 |
|
.sp |
589 |
|
(foo)\eKbar |
590 |
|
.sp |
591 |
|
matches "foobar", the first substring is still set to "foo". |
592 |
|
. |
593 |
|
. |
594 |
.\" HTML <a name="smallassertions"></a> |
.\" HTML <a name="smallassertions"></a> |
595 |
.SS "Simple assertions" |
.SS "Simple assertions" |
596 |
.rs |
.rs |
813 |
If a range that includes letters is used when caseless matching is set, it |
If a range that includes letters is used when caseless matching is set, it |
814 |
matches the letters in either case. For example, [W-c] is equivalent to |
matches the letters in either case. For example, [W-c] is equivalent to |
815 |
[][\e\e^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character |
[][\e\e^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character |
816 |
tables for the "fr_FR" locale are in use, [\exc8-\excb] matches accented E |
tables for a French locale are in use, [\exc8-\excb] matches accented E |
817 |
characters in both cases. In UTF-8 mode, PCRE supports the concept of case for |
characters in both cases. In UTF-8 mode, PCRE supports the concept of case for |
818 |
characters with values greater than 128 only when it is compiled with Unicode |
characters with values greater than 128 only when it is compiled with Unicode |
819 |
property support. |
property support. |
998 |
the above patterns match "SUNDAY" as well as "Saturday". |
the above patterns match "SUNDAY" as well as "Saturday". |
999 |
. |
. |
1000 |
. |
. |
1001 |
|
.SH "DUPLICATE SUBPATTERN NUMBERS" |
1002 |
|
.rs |
1003 |
|
.sp |
1004 |
|
Perl 5.10 introduced a feature whereby each alternative in a subpattern uses |
1005 |
|
the same numbers for its capturing parentheses. Such a subpattern starts with |
1006 |
|
(?| and is itself a non-capturing subpattern. For example, consider this |
1007 |
|
pattern: |
1008 |
|
.sp |
1009 |
|
(?|(Sat)ur|(Sun))day |
1010 |
|
.sp |
1011 |
|
Because the two alternatives are inside a (?| group, both sets of capturing |
1012 |
|
parentheses are numbered one. Thus, when the pattern matches, you can look |
1013 |
|
at captured substring number one, whichever alternative matched. This construct |
1014 |
|
is useful when you want to capture part, but not all, of one of a number of |
1015 |
|
alternatives. Inside a (?| group, parentheses are numbered as usual, but the |
1016 |
|
number is reset at the start of each branch. The numbers of any capturing |
1017 |
|
buffers that follow the subpattern start after the highest number used in any |
1018 |
|
branch. The following example is taken from the Perl documentation. |
1019 |
|
The numbers underneath show in which buffer the captured content will be |
1020 |
|
stored. |
1021 |
|
.sp |
1022 |
|
# before ---------------branch-reset----------- after |
1023 |
|
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
1024 |
|
# 1 2 2 3 2 3 4 |
1025 |
|
.sp |
1026 |
|
A backreference or a recursive call to a numbered subpattern always refers to |
1027 |
|
the first one in the pattern with the given number. |
1028 |
|
.P |
1029 |
|
An alternative approach to using this "branch reset" feature is to use |
1030 |
|
duplicate named subpatterns, as described in the next section. |
1031 |
|
. |
1032 |
|
. |
1033 |
.SH "NAMED SUBPATTERNS" |
.SH "NAMED SUBPATTERNS" |
1034 |
.rs |
.rs |
1035 |
.sp |
.sp |
1079 |
(?<DN>Sat)(?:urday)? |
(?<DN>Sat)(?:urday)? |
1080 |
.sp |
.sp |
1081 |
There are five capturing substrings, but only one is ever set after a match. |
There are five capturing substrings, but only one is ever set after a match. |
1082 |
|
(An alternative way of solving this problem is to use a "branch reset" |
1083 |
|
subpattern, as described in the previous section.) |
1084 |
|
.P |
1085 |
The convenience function for extracting the data by name returns the substring |
The convenience function for extracting the data by name returns the substring |
1086 |
for the first (and in this example, the only) subpattern of that name that |
for the first (and in this example, the only) subpattern of that name that |
1087 |
matched. This saves searching to find which numbered subpattern it was. If you |
matched. This saves searching to find which numbered subpattern it was. If you |
1400 |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original |
1401 |
capturing subpattern is matched caselessly. |
capturing subpattern is matched caselessly. |
1402 |
.P |
.P |
1403 |
Back references to named subpatterns use the Perl syntax \ek<name> or \ek'name' |
There are several different ways of writing back references to named |
1404 |
or the Python syntax (?P=name). We could rewrite the above example in either of |
subpatterns. The .NET syntax \ek{name} and the Perl syntax \ek<name> or |
1405 |
|
\ek'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified |
1406 |
|
back reference syntax, in which \eg can be used for both numeric and named |
1407 |
|
references, is also supported. We could rewrite the above example in any of |
1408 |
the following ways: |
the following ways: |
1409 |
.sp |
.sp |
1410 |
(?<p1>(?i)rah)\es+\ek<p1> |
(?<p1>(?i)rah)\es+\ek<p1> |
1411 |
|
(?'p1'(?i)rah)\es+\ek{p1} |
1412 |
(?P<p1>(?i)rah)\es+(?P=p1) |
(?P<p1>(?i)rah)\es+(?P=p1) |
1413 |
|
(?<p1>(?i)rah)\es+\eg{p1} |
1414 |
.sp |
.sp |
1415 |
A subpattern that is referenced by name may appear in the pattern before or |
A subpattern that is referenced by name may appear in the pattern before or |
1416 |
after the reference. |
after the reference. |
1533 |
.sp |
.sp |
1534 |
(?<=abc|abde) |
(?<=abc|abde) |
1535 |
.sp |
.sp |
1536 |
|
In some cases, the Perl 5.10 escape sequence \eK |
1537 |
|
.\" HTML <a href="#resetmatchstart"> |
1538 |
|
.\" </a> |
1539 |
|
(see above) |
1540 |
|
.\" |
1541 |
|
can be used instead of a lookbehind assertion; this is not restricted to a |
1542 |
|
fixed-length. |
1543 |
|
.P |
1544 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
1545 |
temporarily move the current position back by the fixed length and then try to |
temporarily move the current position back by the fixed length and then try to |
1546 |
match. If there are insufficient characters before the current position, the |
match. If there are insufficient characters before the current position, the |
1635 |
.sp |
.sp |
1636 |
If the text between the parentheses consists of a sequence of digits, the |
If the text between the parentheses consists of a sequence of digits, the |
1637 |
condition is true if the capturing subpattern of that number has previously |
condition is true if the capturing subpattern of that number has previously |
1638 |
matched. |
matched. An alternative notation is to precede the digits with a plus or minus |
1639 |
|
sign. In this case, the subpattern number is relative rather than absolute. |
1640 |
|
The most recently opened parentheses can be referenced by (?(-1), the next most |
1641 |
|
recent by (?(-2), and so on. In looping constructs it can also make sense to |
1642 |
|
refer to subsequent groups with constructs such as (?(+2). |
1643 |
.P |
.P |
1644 |
Consider the following pattern, which contains non-significant white space to |
Consider the following pattern, which contains non-significant white space to |
1645 |
make it more readable (assume the PCRE_EXTENDED option) and to divide it into |
make it more readable (assume the PCRE_EXTENDED option) and to divide it into |
1656 |
parenthesis is required. Otherwise, since no-pattern is not present, the |
parenthesis is required. Otherwise, since no-pattern is not present, the |
1657 |
subpattern matches nothing. In other words, this pattern matches a sequence of |
subpattern matches nothing. In other words, this pattern matches a sequence of |
1658 |
non-parentheses, optionally enclosed in parentheses. |
non-parentheses, optionally enclosed in parentheses. |
1659 |
|
.P |
1660 |
|
If you were embedding this pattern in a larger one, you could use a relative |
1661 |
|
reference: |
1662 |
|
.sp |
1663 |
|
...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ... |
1664 |
|
.sp |
1665 |
|
This makes the fragment independent of the parentheses in the larger pattern. |
1666 |
. |
. |
1667 |
.SS "Checking for a used subpattern by name" |
.SS "Checking for a used subpattern by name" |
1668 |
.rs |
.rs |
1805 |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
1806 |
.sp |
.sp |
1807 |
We have put the pattern into parentheses, and caused the recursion to refer to |
We have put the pattern into parentheses, and caused the recursion to refer to |
1808 |
them instead of the whole pattern. In a larger pattern, keeping track of |
them instead of the whole pattern. |
1809 |
parenthesis numbers can be tricky. It may be more convenient to use named |
.P |
1810 |
parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier |
In a larger pattern, keeping track of parenthesis numbers can be tricky. This |
1811 |
syntax (?P>name) is also supported. We could rewrite the above example as |
is made easier by the use of relative references. (A Perl 5.10 feature.) |
1812 |
follows: |
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
1813 |
|
most recently opened parentheses preceding the recursion. In other words, a |
1814 |
|
negative number counts capturing parentheses leftwards from the point at which |
1815 |
|
it is encountered. |
1816 |
|
.P |
1817 |
|
It is also possible to refer to subsequently opened parentheses, by writing |
1818 |
|
references such as (?+2). However, these cannot be recursive because the |
1819 |
|
reference is not inside the parentheses that are referenced. They are always |
1820 |
|
"subroutine" calls, as described in the next section. |
1821 |
|
.P |
1822 |
|
An alternative approach is to use named parentheses instead. The Perl syntax |
1823 |
|
for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We |
1824 |
|
could rewrite the above example as follows: |
1825 |
.sp |
.sp |
1826 |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
1827 |
.sp |
.sp |
1828 |
If there is more than one subpattern with the same name, the earliest one is |
If there is more than one subpattern with the same name, the earliest one is |
1829 |
used. This particular example pattern contains nested unlimited repeats, and so |
used. |
1830 |
the use of atomic grouping for matching strings of non-parentheses is important |
.P |
1831 |
when applying the pattern to strings that do not match. For example, when this |
This particular example pattern that we have been looking at contains nested |
1832 |
pattern is applied to |
unlimited repeats, and so the use of atomic grouping for matching strings of |
1833 |
|
non-parentheses is important when applying the pattern to strings that do not |
1834 |
|
match. For example, when this pattern is applied to |
1835 |
.sp |
.sp |
1836 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
1837 |
.sp |
.sp |
1883 |
If the syntax for a recursive subpattern reference (either by number or by |
If the syntax for a recursive subpattern reference (either by number or by |
1884 |
name) is used outside the parentheses to which it refers, it operates like a |
name) is used outside the parentheses to which it refers, it operates like a |
1885 |
subroutine in a programming language. The "called" subpattern may be defined |
subroutine in a programming language. The "called" subpattern may be defined |
1886 |
before or after the reference. An earlier example pointed out that the pattern |
before or after the reference. A numbered reference can be absolute or |
1887 |
|
relative, as in these examples: |
1888 |
|
.sp |
1889 |
|
(...(absolute)...)...(?2)... |
1890 |
|
(...(relative)...)...(?-1)... |
1891 |
|
(...(?+1)...(relative)... |
1892 |
|
.sp |
1893 |
|
An earlier example pointed out that the pattern |
1894 |
.sp |
.sp |
1895 |
(sens|respons)e and \e1ibility |
(sens|respons)e and \e1ibility |
1896 |
.sp |
.sp |
1911 |
case-independence are fixed when the subpattern is defined. They cannot be |
case-independence are fixed when the subpattern is defined. They cannot be |
1912 |
changed for different calls. For example, consider this pattern: |
changed for different calls. For example, consider this pattern: |
1913 |
.sp |
.sp |
1914 |
(abc)(?i:(?1)) |
(abc)(?i:(?-1)) |
1915 |
.sp |
.sp |
1916 |
It matches "abcabc". It does not match "abcABC" because the change of |
It matches "abcabc". It does not match "abcABC" because the change of |
1917 |
processing option does not affect the called subpattern. |
processing option does not affect the called subpattern. |
1935 |
can put a number less than 256 after the letter C. The default value is zero. |
can put a number less than 256 after the letter C. The default value is zero. |
1936 |
For example, this pattern has two callout points: |
For example, this pattern has two callout points: |
1937 |
.sp |
.sp |
1938 |
(?C1)\dabc(?C2)def |
(?C1)abc(?C2)def |
1939 |
.sp |
.sp |
1940 |
If the PCRE_AUTO_CALLOUT flag is passed to \fBpcre_compile()\fP, callouts are |
If the PCRE_AUTO_CALLOUT flag is passed to \fBpcre_compile()\fP, callouts are |
1941 |
automatically installed before each item in the pattern. They are all numbered |
automatically installed before each item in the pattern. They are all numbered |
1957 |
.rs |
.rs |
1958 |
.sp |
.sp |
1959 |
\fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3). |
\fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3). |
1960 |
.P |
. |
1961 |
.in 0 |
. |
1962 |
Last updated: 06 December 2006 |
.SH AUTHOR |
1963 |
.br |
.rs |
1964 |
Copyright (c) 1997-2006 University of Cambridge. |
.sp |
1965 |
|
.nf |
1966 |
|
Philip Hazel |
1967 |
|
University Computing Service |
1968 |
|
Cambridge CB2 3QH, England. |
1969 |
|
.fi |
1970 |
|
. |
1971 |
|
. |
1972 |
|
.SH REVISION |
1973 |
|
.rs |
1974 |
|
.sp |
1975 |
|
.nf |
1976 |
|
Last updated: 13 June 2007 |
1977 |
|
Copyright (c) 1997-2007 University of Cambridge. |
1978 |
|
.fi |