| 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, |
| 293 |
.\" HREF |
.\" HREF |
| 294 |
\fBpcreapi\fP |
\fBpcreapi\fP |
| 295 |
.\" |
.\" |
| 296 |
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, |
| 297 |
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 |
| 298 |
|
accented letters, and these are matched by \ew. |
| 299 |
.P |
.P |
| 300 |
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
| 301 |
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
| 521 |
properties in PCRE. |
properties in PCRE. |
| 522 |
. |
. |
| 523 |
. |
. |
| 524 |
|
.\" HTML <a name="resetmatchstart"></a> |
| 525 |
|
.SS "Resetting the match start" |
| 526 |
|
.rs |
| 527 |
|
.sp |
| 528 |
|
The escape sequence \eK, which is a Perl 5.10 feature, causes any previously |
| 529 |
|
matched characters not to be included in the final matched sequence. For |
| 530 |
|
example, the pattern: |
| 531 |
|
.sp |
| 532 |
|
foo\eKbar |
| 533 |
|
.sp |
| 534 |
|
matches "foobar", but reports that it has matched "bar". This feature is |
| 535 |
|
similar to a lookbehind assertion |
| 536 |
|
.\" HTML <a href="#lookbehind"> |
| 537 |
|
.\" </a> |
| 538 |
|
(described below). |
| 539 |
|
.\" |
| 540 |
|
However, in this case, the part of the subject before the real match does not |
| 541 |
|
have to be of fixed length, as lookbehind assertions do. The use of \eK does |
| 542 |
|
not interfere with the setting of |
| 543 |
|
.\" HTML <a href="#subpattern"> |
| 544 |
|
.\" </a> |
| 545 |
|
captured substrings. |
| 546 |
|
.\" |
| 547 |
|
For example, when the pattern |
| 548 |
|
.sp |
| 549 |
|
(foo)\eKbar |
| 550 |
|
.sp |
| 551 |
|
matches "foobar", the first substring is still set to "foo". |
| 552 |
|
. |
| 553 |
|
. |
| 554 |
.\" HTML <a name="smallassertions"></a> |
.\" HTML <a name="smallassertions"></a> |
| 555 |
.SS "Simple assertions" |
.SS "Simple assertions" |
| 556 |
.rs |
.rs |
| 773 |
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 |
| 774 |
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 |
| 775 |
[][\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 |
| 776 |
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 |
| 777 |
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 |
| 778 |
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 |
| 779 |
property support. |
property support. |
| 1325 |
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 |
| 1326 |
capturing subpattern is matched caselessly. |
capturing subpattern is matched caselessly. |
| 1327 |
.P |
.P |
| 1328 |
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 |
| 1329 |
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 |
| 1330 |
|
\ek'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified |
| 1331 |
|
back reference syntax, in which \eg can be used for both numeric and named |
| 1332 |
|
references, is also supported. We could rewrite the above example in any of |
| 1333 |
the following ways: |
the following ways: |
| 1334 |
.sp |
.sp |
| 1335 |
(?<p1>(?i)rah)\es+\ek<p1> |
(?<p1>(?i)rah)\es+\ek<p1> |
| 1336 |
|
(?'p1'(?i)rah)\es+\ek{p1} |
| 1337 |
(?P<p1>(?i)rah)\es+(?P=p1) |
(?P<p1>(?i)rah)\es+(?P=p1) |
| 1338 |
|
(?<p1>(?i)rah)\es+\eg{p1} |
| 1339 |
.sp |
.sp |
| 1340 |
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 |
| 1341 |
after the reference. |
after the reference. |
| 1458 |
.sp |
.sp |
| 1459 |
(?<=abc|abde) |
(?<=abc|abde) |
| 1460 |
.sp |
.sp |
| 1461 |
|
In some cases, the Perl 5.10 escape sequence \eK |
| 1462 |
|
.\" HTML <a href="#resetmatchstart"> |
| 1463 |
|
.\" </a> |
| 1464 |
|
(see above) |
| 1465 |
|
.\" |
| 1466 |
|
can be used instead of a lookbehind assertion; this is not restricted to a |
| 1467 |
|
fixed-length. |
| 1468 |
|
.P |
| 1469 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
| 1470 |
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 |
| 1471 |
match. If there are insufficient characters before the current position, the |
match. If there are insufficient characters before the current position, the |
| 1560 |
.sp |
.sp |
| 1561 |
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 |
| 1562 |
condition is true if the capturing subpattern of that number has previously |
condition is true if the capturing subpattern of that number has previously |
| 1563 |
matched. |
matched. An alternative notation is to precede the digits with a plus or minus |
| 1564 |
|
sign. In this case, the subpattern number is relative rather than absolute. |
| 1565 |
|
The most recently opened parentheses can be referenced by (?(-1), the next most |
| 1566 |
|
recent by (?(-2), and so on. In looping constructs it can also make sense to |
| 1567 |
|
refer to subsequent groups with constructs such as (?(+2). |
| 1568 |
.P |
.P |
| 1569 |
Consider the following pattern, which contains non-significant white space to |
Consider the following pattern, which contains non-significant white space to |
| 1570 |
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 |
| 1581 |
parenthesis is required. Otherwise, since no-pattern is not present, the |
parenthesis is required. Otherwise, since no-pattern is not present, the |
| 1582 |
subpattern matches nothing. In other words, this pattern matches a sequence of |
subpattern matches nothing. In other words, this pattern matches a sequence of |
| 1583 |
non-parentheses, optionally enclosed in parentheses. |
non-parentheses, optionally enclosed in parentheses. |
| 1584 |
|
.P |
| 1585 |
|
If you were embedding this pattern in a larger one, you could use a relative |
| 1586 |
|
reference: |
| 1587 |
|
.sp |
| 1588 |
|
...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ... |
| 1589 |
|
.sp |
| 1590 |
|
This makes the fragment independent of the parentheses in the larger pattern. |
| 1591 |
. |
. |
| 1592 |
.SS "Checking for a used subpattern by name" |
.SS "Checking for a used subpattern by name" |
| 1593 |
.rs |
.rs |
| 1730 |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
| 1731 |
.sp |
.sp |
| 1732 |
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 |
| 1733 |
them instead of the whole pattern. In a larger pattern, keeping track of |
them instead of the whole pattern. |
| 1734 |
parenthesis numbers can be tricky. It may be more convenient to use named |
.P |
| 1735 |
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 |
| 1736 |
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.) |
| 1737 |
follows: |
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
| 1738 |
|
most recently opened parentheses preceding the recursion. In other words, a |
| 1739 |
|
negative number counts capturing parentheses leftwards from the point at which |
| 1740 |
|
it is encountered. |
| 1741 |
|
.P |
| 1742 |
|
It is also possible to refer to subsequently opened parentheses, by writing |
| 1743 |
|
references such as (?+2). However, these cannot be recursive because the |
| 1744 |
|
reference is not inside the parentheses that are referenced. They are always |
| 1745 |
|
"subroutine" calls, as described in the next section. |
| 1746 |
|
.P |
| 1747 |
|
An alternative approach is to use named parentheses instead. The Perl syntax |
| 1748 |
|
for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We |
| 1749 |
|
could rewrite the above example as follows: |
| 1750 |
.sp |
.sp |
| 1751 |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
| 1752 |
.sp |
.sp |
| 1753 |
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 |
| 1754 |
used. This particular example pattern contains nested unlimited repeats, and so |
used. |
| 1755 |
the use of atomic grouping for matching strings of non-parentheses is important |
.P |
| 1756 |
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 |
| 1757 |
pattern is applied to |
unlimited repeats, and so the use of atomic grouping for matching strings of |
| 1758 |
|
non-parentheses is important when applying the pattern to strings that do not |
| 1759 |
|
match. For example, when this pattern is applied to |
| 1760 |
.sp |
.sp |
| 1761 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 1762 |
.sp |
.sp |
| 1808 |
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 |
| 1809 |
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 |
| 1810 |
subroutine in a programming language. The "called" subpattern may be defined |
subroutine in a programming language. The "called" subpattern may be defined |
| 1811 |
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 |
| 1812 |
|
relative, as in these examples: |
| 1813 |
|
.sp |
| 1814 |
|
(...(absolute)...)...(?2)... |
| 1815 |
|
(...(relative)...)...(?-1)... |
| 1816 |
|
(...(?+1)...(relative)... |
| 1817 |
|
.sp |
| 1818 |
|
An earlier example pointed out that the pattern |
| 1819 |
.sp |
.sp |
| 1820 |
(sens|respons)e and \e1ibility |
(sens|respons)e and \e1ibility |
| 1821 |
.sp |
.sp |
| 1836 |
case-independence are fixed when the subpattern is defined. They cannot be |
case-independence are fixed when the subpattern is defined. They cannot be |
| 1837 |
changed for different calls. For example, consider this pattern: |
changed for different calls. For example, consider this pattern: |
| 1838 |
.sp |
.sp |
| 1839 |
(abc)(?i:(?1)) |
(abc)(?i:(?-1)) |
| 1840 |
.sp |
.sp |
| 1841 |
It matches "abcabc". It does not match "abcABC" because the change of |
It matches "abcabc". It does not match "abcABC" because the change of |
| 1842 |
processing option does not affect the called subpattern. |
processing option does not affect the called subpattern. |
| 1860 |
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. |
| 1861 |
For example, this pattern has two callout points: |
For example, this pattern has two callout points: |
| 1862 |
.sp |
.sp |
| 1863 |
(?C1)\dabc(?C2)def |
(?C1)abc(?C2)def |
| 1864 |
.sp |
.sp |
| 1865 |
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 |
| 1866 |
automatically installed before each item in the pattern. They are all numbered |
automatically installed before each item in the pattern. They are all numbered |
| 1882 |
.rs |
.rs |
| 1883 |
.sp |
.sp |
| 1884 |
\fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3). |
\fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3). |
| 1885 |
.P |
. |
| 1886 |
.in 0 |
. |
| 1887 |
Last updated: 06 December 2006 |
.SH AUTHOR |
| 1888 |
.br |
.rs |
| 1889 |
Copyright (c) 1997-2006 University of Cambridge. |
.sp |
| 1890 |
|
.nf |
| 1891 |
|
Philip Hazel |
| 1892 |
|
University Computing Service |
| 1893 |
|
Cambridge CB2 3QH, England. |
| 1894 |
|
.fi |
| 1895 |
|
. |
| 1896 |
|
. |
| 1897 |
|
.SH REVISION |
| 1898 |
|
.rs |
| 1899 |
|
.sp |
| 1900 |
|
.nf |
| 1901 |
|
Last updated: 29 May 2007 |
| 1902 |
|
Copyright (c) 1997-2007 University of Cambridge. |
| 1903 |
|
.fi |