| 1674 |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
| 1675 |
.sp |
.sp |
| 1676 |
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 |
| 1677 |
them instead of the whole pattern. In a larger pattern, keeping track of |
them instead of the whole pattern. |
| 1678 |
parenthesis numbers can be tricky. It may be more convenient to use named |
.P |
| 1679 |
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 |
| 1680 |
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.) |
| 1681 |
follows: |
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
| 1682 |
|
most recently opened parentheses preceding the recursion. In other words, a |
| 1683 |
|
negative number counts capturing parentheses leftwards from the point at which |
| 1684 |
|
it is encountered. |
| 1685 |
|
.P |
| 1686 |
|
It is also possible to refer to subsequently opened parentheses, by writing |
| 1687 |
|
references such as (?+2). However, these cannot be recursive because the |
| 1688 |
|
reference is not inside the parentheses that are referenced. They are always |
| 1689 |
|
"subroutine" calls, as described in the next section. |
| 1690 |
|
.P |
| 1691 |
|
An alternative approach is to use named parentheses instead. The Perl syntax |
| 1692 |
|
for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We |
| 1693 |
|
could rewrite the above example as follows: |
| 1694 |
.sp |
.sp |
| 1695 |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
| 1696 |
.sp |
.sp |
| 1697 |
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 |
| 1698 |
used. This particular example pattern contains nested unlimited repeats, and so |
used. |
| 1699 |
the use of atomic grouping for matching strings of non-parentheses is important |
.P |
| 1700 |
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 |
| 1701 |
pattern is applied to |
unlimited repeats, and so the use of atomic grouping for matching strings of |
| 1702 |
|
non-parentheses is important when applying the pattern to strings that do not |
| 1703 |
|
match. For example, when this pattern is applied to |
| 1704 |
.sp |
.sp |
| 1705 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 1706 |
.sp |
.sp |
| 1752 |
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 |
| 1753 |
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 |
| 1754 |
subroutine in a programming language. The "called" subpattern may be defined |
subroutine in a programming language. The "called" subpattern may be defined |
| 1755 |
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 |
| 1756 |
|
relative, as in these examples: |
| 1757 |
|
.sp |
| 1758 |
|
(...(absolute)...)...(?2)... |
| 1759 |
|
(...(relative)...)...(?-1)... |
| 1760 |
|
(...(?+1)...(relative)... |
| 1761 |
|
.sp |
| 1762 |
|
An earlier example pointed out that the pattern |
| 1763 |
.sp |
.sp |
| 1764 |
(sens|respons)e and \e1ibility |
(sens|respons)e and \e1ibility |
| 1765 |
.sp |
.sp |
| 1780 |
case-independence are fixed when the subpattern is defined. They cannot be |
case-independence are fixed when the subpattern is defined. They cannot be |
| 1781 |
changed for different calls. For example, consider this pattern: |
changed for different calls. For example, consider this pattern: |
| 1782 |
.sp |
.sp |
| 1783 |
(abc)(?i:(?1)) |
(abc)(?i:(?-1)) |
| 1784 |
.sp |
.sp |
| 1785 |
It matches "abcabc". It does not match "abcABC" because the change of |
It matches "abcabc". It does not match "abcABC" because the change of |
| 1786 |
processing option does not affect the called subpattern. |
processing option does not affect the called subpattern. |