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, |
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 |
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. An alternative notation is to precede the digits with a plus or minus |
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. |
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 |
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 |
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). |
refer to subsequent groups with constructs such as (?(+2). |
1568 |
.P |
.P |
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 |
.P |
1585 |
If you were embedding this pattern in a larger one, you could use a relative |
If you were embedding this pattern in a larger one, you could use a relative |
1586 |
reference: |
reference: |
1587 |
.sp |
.sp |
1588 |
...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ... |
...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ... |
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. |
them instead of the whole pattern. |
1734 |
.P |
.P |
1735 |
In a larger pattern, keeping track of parenthesis numbers can be tricky. This |
In a larger pattern, keeping track of parenthesis numbers can be tricky. This |
1736 |
is made easier by the use of relative references. (A Perl 5.10 feature.) |
is made easier by the use of relative references. (A Perl 5.10 feature.) |
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. |
used. |
1755 |
.P |
.P |
1756 |
This particular example pattern that we have been looking at contains nested |
This particular example pattern that we have been looking at contains nested |
1757 |
unlimited repeats, and so the use of atomic grouping for matching strings of |
unlimited repeats, and so the use of atomic grouping for matching strings of |
1813 |
.sp |
.sp |
1814 |
(...(absolute)...)...(?2)... |
(...(absolute)...)...(?2)... |
1815 |
(...(relative)...)...(?-1)... |
(...(relative)...)...(?-1)... |
1816 |
(...(?+1)...(relative)... |
(...(?+1)...(relative)... |
1817 |
.sp |
.sp |
1818 |
An earlier example pointed out that the pattern |
An earlier example pointed out that the pattern |
1819 |
.sp |
.sp |
1898 |
.rs |
.rs |
1899 |
.sp |
.sp |
1900 |
.nf |
.nf |
1901 |
Last updated: 09 May 2007 |
Last updated: 29 May 2007 |
1902 |
Copyright (c) 1997-2007 University of Cambridge. |
Copyright (c) 1997-2007 University of Cambridge. |
1903 |
.fi |
.fi |