29 |
5.10, including support for UTF-8 encoded strings and Unicode general |
5.10, including support for UTF-8 encoded strings and Unicode general |
30 |
category properties. However, UTF-8 and Unicode support has to be |
category properties. However, UTF-8 and Unicode support has to be |
31 |
explicitly enabled; it is not the default. The Unicode tables corre- |
explicitly enabled; it is not the default. The Unicode tables corre- |
32 |
spond to Unicode release 5.1. |
spond to Unicode release 5.2.0. |
33 |
|
|
34 |
In addition to the Perl-compatible matching function, PCRE contains an |
In addition to the Perl-compatible matching function, PCRE contains an |
35 |
alternative function that matches the same compiled patterns in a dif- |
alternative function that matches the same compiled patterns in a dif- |
263 |
|
|
264 |
REVISION |
REVISION |
265 |
|
|
266 |
Last updated: 28 September 2009 |
Last updated: 01 March 2010 |
267 |
Copyright (c) 1997-2009 University of Cambridge. |
Copyright (c) 1997-2010 University of Cambridge. |
268 |
------------------------------------------------------------------------------ |
------------------------------------------------------------------------------ |
269 |
|
|
270 |
|
|
3488 |
Those that are not part of an identified script are lumped together as |
Those that are not part of an identified script are lumped together as |
3489 |
"Common". The current list of scripts is: |
"Common". The current list of scripts is: |
3490 |
|
|
3491 |
Arabic, Armenian, Balinese, Bengali, Bopomofo, Braille, Buginese, |
Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille, |
3492 |
Buhid, Canadian_Aboriginal, Cherokee, Common, Coptic, Cuneiform, |
Buginese, Buhid, Canadian_Aboriginal, Carian, Cham, Cherokee, Common, |
3493 |
Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic, |
Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, Devanagari, Egyp- |
3494 |
Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira- |
tian_Hieroglyphs, Ethiopic, Georgian, Glagolitic, Gothic, Greek, |
3495 |
gana, Inherited, Kannada, Katakana, Kharoshthi, Khmer, Lao, Latin, |
Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Impe- |
3496 |
Limbu, Linear_B, Malayalam, Mongolian, Myanmar, New_Tai_Lue, Nko, |
rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian, |
3497 |
Ogham, Old_Italic, Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician, |
Javanese, Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao, |
3498 |
Runic, Shavian, Sinhala, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, |
Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian, Lydian, Malayalam, |
3499 |
Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi. |
Meetei_Mayek, Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic, |
3500 |
|
Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki, Oriya, Osmanya, |
3501 |
|
Phags_Pa, Phoenician, Rejang, Runic, Samaritan, Saurashtra, Shavian, |
3502 |
|
Sinhala, Sundanese, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, |
3503 |
|
Tai_Tham, Tai_Viet, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, |
3504 |
|
Ugaritic, Vai, Yi. |
3505 |
|
|
3506 |
Each character has exactly one general category property, specified by |
Each character has exactly one general category property, specified by |
3507 |
a two-letter abbreviation. For compatibility with Perl, negation can be |
a two-letter abbreviation. For compatibility with Perl, negation can be |
3508 |
specified by including a circumflex between the opening brace and the |
specified by including a circumflex between the opening brace and the |
3509 |
property name. For example, \p{^Lu} is the same as \P{Lu}. |
property name. For example, \p{^Lu} is the same as \P{Lu}. |
3510 |
|
|
3511 |
If only one letter is specified with \p or \P, it includes all the gen- |
If only one letter is specified with \p or \P, it includes all the gen- |
3512 |
eral category properties that start with that letter. In this case, in |
eral category properties that start with that letter. In this case, in |
3513 |
the absence of negation, the curly brackets in the escape sequence are |
the absence of negation, the curly brackets in the escape sequence are |
3514 |
optional; these two examples have the same effect: |
optional; these two examples have the same effect: |
3515 |
|
|
3516 |
\p{L} |
\p{L} |
3562 |
Zp Paragraph separator |
Zp Paragraph separator |
3563 |
Zs Space separator |
Zs Space separator |
3564 |
|
|
3565 |
The special property L& is also supported: it matches a character that |
The special property L& is also supported: it matches a character that |
3566 |
has the Lu, Ll, or Lt property, in other words, a letter that is not |
has the Lu, Ll, or Lt property, in other words, a letter that is not |
3567 |
classified as a modifier or "other". |
classified as a modifier or "other". |
3568 |
|
|
3569 |
The Cs (Surrogate) property applies only to characters in the range |
The Cs (Surrogate) property applies only to characters in the range |
3570 |
U+D800 to U+DFFF. Such characters are not valid in UTF-8 strings (see |
U+D800 to U+DFFF. Such characters are not valid in UTF-8 strings (see |
3571 |
RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check- |
RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check- |
3572 |
ing has been turned off (see the discussion of PCRE_NO_UTF8_CHECK in |
ing has been turned off (see the discussion of PCRE_NO_UTF8_CHECK in |
3573 |
the pcreapi page). Perl does not support the Cs property. |
the pcreapi page). Perl does not support the Cs property. |
3574 |
|
|
3575 |
The long synonyms for property names that Perl supports (such as |
The long synonyms for property names that Perl supports (such as |
3576 |
\p{Letter}) are not supported by PCRE, nor is it permitted to prefix |
\p{Letter}) are not supported by PCRE, nor is it permitted to prefix |
3577 |
any of these properties with "Is". |
any of these properties with "Is". |
3578 |
|
|
3579 |
No character that is in the Unicode table has the Cn (unassigned) prop- |
No character that is in the Unicode table has the Cn (unassigned) prop- |
3580 |
erty. Instead, this property is assumed for any code point that is not |
erty. Instead, this property is assumed for any code point that is not |
3581 |
in the Unicode table. |
in the Unicode table. |
3582 |
|
|
3583 |
Specifying caseless matching does not affect these escape sequences. |
Specifying caseless matching does not affect these escape sequences. |
3584 |
For example, \p{Lu} always matches only upper case letters. |
For example, \p{Lu} always matches only upper case letters. |
3585 |
|
|
3586 |
The \X escape matches any number of Unicode characters that form an |
The \X escape matches any number of Unicode characters that form an |
3587 |
extended Unicode sequence. \X is equivalent to |
extended Unicode sequence. \X is equivalent to |
3588 |
|
|
3589 |
(?>\PM\pM*) |
(?>\PM\pM*) |
3590 |
|
|
3591 |
That is, it matches a character without the "mark" property, followed |
That is, it matches a character without the "mark" property, followed |
3592 |
by zero or more characters with the "mark" property, and treats the |
by zero or more characters with the "mark" property, and treats the |
3593 |
sequence as an atomic group (see below). Characters with the "mark" |
sequence as an atomic group (see below). Characters with the "mark" |
3594 |
property are typically accents that affect the preceding character. |
property are typically accents that affect the preceding character. |
3595 |
None of them have codepoints less than 256, so in non-UTF-8 mode \X |
None of them have codepoints less than 256, so in non-UTF-8 mode \X |
3596 |
matches any one character. |
matches any one character. |
3597 |
|
|
3598 |
Matching characters by Unicode property is not fast, because PCRE has |
Matching characters by Unicode property is not fast, because PCRE has |
3599 |
to search a structure that contains data for over fifteen thousand |
to search a structure that contains data for over fifteen thousand |
3600 |
characters. That is why the traditional escape sequences such as \d and |
characters. That is why the traditional escape sequences such as \d and |
3601 |
\w do not use Unicode properties in PCRE. |
\w do not use Unicode properties in PCRE. |
3602 |
|
|
3603 |
Resetting the match start |
Resetting the match start |
3604 |
|
|
3605 |
The escape sequence \K, which is a Perl 5.10 feature, causes any previ- |
The escape sequence \K, which is a Perl 5.10 feature, causes any previ- |
3606 |
ously matched characters not to be included in the final matched |
ously matched characters not to be included in the final matched |
3607 |
sequence. For example, the pattern: |
sequence. For example, the pattern: |
3608 |
|
|
3609 |
foo\Kbar |
foo\Kbar |
3610 |
|
|
3611 |
matches "foobar", but reports that it has matched "bar". This feature |
matches "foobar", but reports that it has matched "bar". This feature |
3612 |
is similar to a lookbehind assertion (described below). However, in |
is similar to a lookbehind assertion (described below). However, in |
3613 |
this case, the part of the subject before the real match does not have |
this case, the part of the subject before the real match does not have |
3614 |
to be of fixed length, as lookbehind assertions do. The use of \K does |
to be of fixed length, as lookbehind assertions do. The use of \K does |
3615 |
not interfere with the setting of captured substrings. For example, |
not interfere with the setting of captured substrings. For example, |
3616 |
when the pattern |
when the pattern |
3617 |
|
|
3618 |
(foo)\Kbar |
(foo)\Kbar |
3619 |
|
|
3620 |
matches "foobar", the first substring is still set to "foo". |
matches "foobar", the first substring is still set to "foo". |
3621 |
|
|
3622 |
|
Perl documents that the use of \K within assertions is "not well |
3623 |
|
defined". In PCRE, \K is acted upon when it occurs inside positive |
3624 |
|
assertions, but is ignored in negative assertions. |
3625 |
|
|
3626 |
Simple assertions |
Simple assertions |
3627 |
|
|
3628 |
The final use of backslash is for certain simple assertions. An asser- |
The final use of backslash is for certain simple assertions. An asser- |
3629 |
tion specifies a condition that has to be met at a particular point in |
tion specifies a condition that has to be met at a particular point in |
3630 |
a match, without consuming any characters from the subject string. The |
a match, without consuming any characters from the subject string. The |
3631 |
use of subpatterns for more complicated assertions is described below. |
use of subpatterns for more complicated assertions is described below. |
3632 |
The backslashed assertions are: |
The backslashed assertions are: |
3633 |
|
|
3634 |
\b matches at a word boundary |
\b matches at a word boundary |
3639 |
\z matches only at the end of the subject |
\z matches only at the end of the subject |
3640 |
\G matches at the first matching position in the subject |
\G matches at the first matching position in the subject |
3641 |
|
|
3642 |
These assertions may not appear in character classes (but note that \b |
These assertions may not appear in character classes (but note that \b |
3643 |
has a different meaning, namely the backspace character, inside a char- |
has a different meaning, namely the backspace character, inside a char- |
3644 |
acter class). |
acter class). |
3645 |
|
|
3646 |
A word boundary is a position in the subject string where the current |
A word boundary is a position in the subject string where the current |
3647 |
character and the previous character do not both match \w or \W (i.e. |
character and the previous character do not both match \w or \W (i.e. |
3648 |
one matches \w and the other matches \W), or the start or end of the |
one matches \w and the other matches \W), or the start or end of the |
3649 |
string if the first or last character matches \w, respectively. Neither |
string if the first or last character matches \w, respectively. Neither |
3650 |
PCRE nor Perl has a separte "start of word" or "end of word" metase- |
PCRE nor Perl has a separte "start of word" or "end of word" metase- |
3651 |
quence. However, whatever follows \b normally determines which it is. |
quence. However, whatever follows \b normally determines which it is. |
3652 |
For example, the fragment \ba matches "a" at the start of a word. |
For example, the fragment \ba matches "a" at the start of a word. |
3653 |
|
|
3654 |
The \A, \Z, and \z assertions differ from the traditional circumflex |
The \A, \Z, and \z assertions differ from the traditional circumflex |
3655 |
and dollar (described in the next section) in that they only ever match |
and dollar (described in the next section) in that they only ever match |
3656 |
at the very start and end of the subject string, whatever options are |
at the very start and end of the subject string, whatever options are |
3657 |
set. Thus, they are independent of multiline mode. These three asser- |
set. Thus, they are independent of multiline mode. These three asser- |
3658 |
tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which |
tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which |
3659 |
affect only the behaviour of the circumflex and dollar metacharacters. |
affect only the behaviour of the circumflex and dollar metacharacters. |
3660 |
However, if the startoffset argument of pcre_exec() is non-zero, indi- |
However, if the startoffset argument of pcre_exec() is non-zero, indi- |
3661 |
cating that matching is to start at a point other than the beginning of |
cating that matching is to start at a point other than the beginning of |
3662 |
the subject, \A can never match. The difference between \Z and \z is |
the subject, \A can never match. The difference between \Z and \z is |
3663 |
that \Z matches before a newline at the end of the string as well as at |
that \Z matches before a newline at the end of the string as well as at |
3664 |
the very end, whereas \z matches only at the end. |
the very end, whereas \z matches only at the end. |
3665 |
|
|
3666 |
The \G assertion is true only when the current matching position is at |
The \G assertion is true only when the current matching position is at |
3667 |
the start point of the match, as specified by the startoffset argument |
the start point of the match, as specified by the startoffset argument |
3668 |
of pcre_exec(). It differs from \A when the value of startoffset is |
of pcre_exec(). It differs from \A when the value of startoffset is |
3669 |
non-zero. By calling pcre_exec() multiple times with appropriate argu- |
non-zero. By calling pcre_exec() multiple times with appropriate argu- |
3670 |
ments, you can mimic Perl's /g option, and it is in this kind of imple- |
ments, you can mimic Perl's /g option, and it is in this kind of imple- |
3671 |
mentation where \G can be useful. |
mentation where \G can be useful. |
3672 |
|
|
3673 |
Note, however, that PCRE's interpretation of \G, as the start of the |
Note, however, that PCRE's interpretation of \G, as the start of the |
3674 |
current match, is subtly different from Perl's, which defines it as the |
current match, is subtly different from Perl's, which defines it as the |
3675 |
end of the previous match. In Perl, these can be different when the |
end of the previous match. In Perl, these can be different when the |
3676 |
previously matched string was empty. Because PCRE does just one match |
previously matched string was empty. Because PCRE does just one match |
3677 |
at a time, it cannot reproduce this behaviour. |
at a time, it cannot reproduce this behaviour. |
3678 |
|
|
3679 |
If all the alternatives of a pattern begin with \G, the expression is |
If all the alternatives of a pattern begin with \G, the expression is |
3680 |
anchored to the starting match position, and the "anchored" flag is set |
anchored to the starting match position, and the "anchored" flag is set |
3681 |
in the compiled regular expression. |
in the compiled regular expression. |
3682 |
|
|
3684 |
CIRCUMFLEX AND DOLLAR |
CIRCUMFLEX AND DOLLAR |
3685 |
|
|
3686 |
Outside a character class, in the default matching mode, the circumflex |
Outside a character class, in the default matching mode, the circumflex |
3687 |
character is an assertion that is true only if the current matching |
character is an assertion that is true only if the current matching |
3688 |
point is at the start of the subject string. If the startoffset argu- |
point is at the start of the subject string. If the startoffset argu- |
3689 |
ment of pcre_exec() is non-zero, circumflex can never match if the |
ment of pcre_exec() is non-zero, circumflex can never match if the |
3690 |
PCRE_MULTILINE option is unset. Inside a character class, circumflex |
PCRE_MULTILINE option is unset. Inside a character class, circumflex |
3691 |
has an entirely different meaning (see below). |
has an entirely different meaning (see below). |
3692 |
|
|
3693 |
Circumflex need not be the first character of the pattern if a number |
Circumflex need not be the first character of the pattern if a number |
3694 |
of alternatives are involved, but it should be the first thing in each |
of alternatives are involved, but it should be the first thing in each |
3695 |
alternative in which it appears if the pattern is ever to match that |
alternative in which it appears if the pattern is ever to match that |
3696 |
branch. If all possible alternatives start with a circumflex, that is, |
branch. If all possible alternatives start with a circumflex, that is, |
3697 |
if the pattern is constrained to match only at the start of the sub- |
if the pattern is constrained to match only at the start of the sub- |
3698 |
ject, it is said to be an "anchored" pattern. (There are also other |
ject, it is said to be an "anchored" pattern. (There are also other |
3699 |
constructs that can cause a pattern to be anchored.) |
constructs that can cause a pattern to be anchored.) |
3700 |
|
|
3701 |
A dollar character is an assertion that is true only if the current |
A dollar character is an assertion that is true only if the current |
3702 |
matching point is at the end of the subject string, or immediately |
matching point is at the end of the subject string, or immediately |
3703 |
before a newline at the end of the string (by default). Dollar need not |
before a newline at the end of the string (by default). Dollar need not |
3704 |
be the last character of the pattern if a number of alternatives are |
be the last character of the pattern if a number of alternatives are |
3705 |
involved, but it should be the last item in any branch in which it |
involved, but it should be the last item in any branch in which it |
3706 |
appears. Dollar has no special meaning in a character class. |
appears. Dollar has no special meaning in a character class. |
3707 |
|
|
3708 |
The meaning of dollar can be changed so that it matches only at the |
The meaning of dollar can be changed so that it matches only at the |
3709 |
very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at |
very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at |
3710 |
compile time. This does not affect the \Z assertion. |
compile time. This does not affect the \Z assertion. |
3711 |
|
|
3712 |
The meanings of the circumflex and dollar characters are changed if the |
The meanings of the circumflex and dollar characters are changed if the |
3713 |
PCRE_MULTILINE option is set. When this is the case, a circumflex |
PCRE_MULTILINE option is set. When this is the case, a circumflex |
3714 |
matches immediately after internal newlines as well as at the start of |
matches immediately after internal newlines as well as at the start of |
3715 |
the subject string. It does not match after a newline that ends the |
the subject string. It does not match after a newline that ends the |
3716 |
string. A dollar matches before any newlines in the string, as well as |
string. A dollar matches before any newlines in the string, as well as |
3717 |
at the very end, when PCRE_MULTILINE is set. When newline is specified |
at the very end, when PCRE_MULTILINE is set. When newline is specified |
3718 |
as the two-character sequence CRLF, isolated CR and LF characters do |
as the two-character sequence CRLF, isolated CR and LF characters do |
3719 |
not indicate newlines. |
not indicate newlines. |
3720 |
|
|
3721 |
For example, the pattern /^abc$/ matches the subject string "def\nabc" |
For example, the pattern /^abc$/ matches the subject string "def\nabc" |
3722 |
(where \n represents a newline) in multiline mode, but not otherwise. |
(where \n represents a newline) in multiline mode, but not otherwise. |
3723 |
Consequently, patterns that are anchored in single line mode because |
Consequently, patterns that are anchored in single line mode because |
3724 |
all branches start with ^ are not anchored in multiline mode, and a |
all branches start with ^ are not anchored in multiline mode, and a |
3725 |
match for circumflex is possible when the startoffset argument of |
match for circumflex is possible when the startoffset argument of |
3726 |
pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if |
pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if |
3727 |
PCRE_MULTILINE is set. |
PCRE_MULTILINE is set. |
3728 |
|
|
3729 |
Note that the sequences \A, \Z, and \z can be used to match the start |
Note that the sequences \A, \Z, and \z can be used to match the start |
3730 |
and end of the subject in both modes, and if all branches of a pattern |
and end of the subject in both modes, and if all branches of a pattern |
3731 |
start with \A it is always anchored, whether or not PCRE_MULTILINE is |
start with \A it is always anchored, whether or not PCRE_MULTILINE is |
3732 |
set. |
set. |
3733 |
|
|
3734 |
|
|
3735 |
FULL STOP (PERIOD, DOT) |
FULL STOP (PERIOD, DOT) |
3736 |
|
|
3737 |
Outside a character class, a dot in the pattern matches any one charac- |
Outside a character class, a dot in the pattern matches any one charac- |
3738 |
ter in the subject string except (by default) a character that signi- |
ter in the subject string except (by default) a character that signi- |
3739 |
fies the end of a line. In UTF-8 mode, the matched character may be |
fies the end of a line. In UTF-8 mode, the matched character may be |
3740 |
more than one byte long. |
more than one byte long. |
3741 |
|
|
3742 |
When a line ending is defined as a single character, dot never matches |
When a line ending is defined as a single character, dot never matches |
3743 |
that character; when the two-character sequence CRLF is used, dot does |
that character; when the two-character sequence CRLF is used, dot does |
3744 |
not match CR if it is immediately followed by LF, but otherwise it |
not match CR if it is immediately followed by LF, but otherwise it |
3745 |
matches all characters (including isolated CRs and LFs). When any Uni- |
matches all characters (including isolated CRs and LFs). When any Uni- |
3746 |
code line endings are being recognized, dot does not match CR or LF or |
code line endings are being recognized, dot does not match CR or LF or |
3747 |
any of the other line ending characters. |
any of the other line ending characters. |
3748 |
|
|
3749 |
The behaviour of dot with regard to newlines can be changed. If the |
The behaviour of dot with regard to newlines can be changed. If the |
3750 |
PCRE_DOTALL option is set, a dot matches any one character, without |
PCRE_DOTALL option is set, a dot matches any one character, without |
3751 |
exception. If the two-character sequence CRLF is present in the subject |
exception. If the two-character sequence CRLF is present in the subject |
3752 |
string, it takes two dots to match it. |
string, it takes two dots to match it. |
3753 |
|
|
3754 |
The handling of dot is entirely independent of the handling of circum- |
The handling of dot is entirely independent of the handling of circum- |
3755 |
flex and dollar, the only relationship being that they both involve |
flex and dollar, the only relationship being that they both involve |
3756 |
newlines. Dot has no special meaning in a character class. |
newlines. Dot has no special meaning in a character class. |
3757 |
|
|
3758 |
|
|
3759 |
MATCHING A SINGLE BYTE |
MATCHING A SINGLE BYTE |
3760 |
|
|
3761 |
Outside a character class, the escape sequence \C matches any one byte, |
Outside a character class, the escape sequence \C matches any one byte, |
3762 |
both in and out of UTF-8 mode. Unlike a dot, it always matches any |
both in and out of UTF-8 mode. Unlike a dot, it always matches any |
3763 |
line-ending characters. The feature is provided in Perl in order to |
line-ending characters. The feature is provided in Perl in order to |
3764 |
match individual bytes in UTF-8 mode. Because it breaks up UTF-8 char- |
match individual bytes in UTF-8 mode. Because it breaks up UTF-8 char- |
3765 |
acters into individual bytes, what remains in the string may be a mal- |
acters into individual bytes, what remains in the string may be a mal- |
3766 |
formed UTF-8 string. For this reason, the \C escape sequence is best |
formed UTF-8 string. For this reason, the \C escape sequence is best |
3767 |
avoided. |
avoided. |
3768 |
|
|
3769 |
PCRE does not allow \C to appear in lookbehind assertions (described |
PCRE does not allow \C to appear in lookbehind assertions (described |
3770 |
below), because in UTF-8 mode this would make it impossible to calcu- |
below), because in UTF-8 mode this would make it impossible to calcu- |
3771 |
late the length of the lookbehind. |
late the length of the lookbehind. |
3772 |
|
|
3773 |
|
|
3777 |
closing square bracket. A closing square bracket on its own is not spe- |
closing square bracket. A closing square bracket on its own is not spe- |
3778 |
cial by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set, |
cial by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set, |
3779 |
a lone closing square bracket causes a compile-time error. If a closing |
a lone closing square bracket causes a compile-time error. If a closing |
3780 |
square bracket is required as a member of the class, it should be the |
square bracket is required as a member of the class, it should be the |
3781 |
first data character in the class (after an initial circumflex, if |
first data character in the class (after an initial circumflex, if |
3782 |
present) or escaped with a backslash. |
present) or escaped with a backslash. |
3783 |
|
|
3784 |
A character class matches a single character in the subject. In UTF-8 |
A character class matches a single character in the subject. In UTF-8 |
3785 |
mode, the character may be more than one byte long. A matched character |
mode, the character may be more than one byte long. A matched character |
3786 |
must be in the set of characters defined by the class, unless the first |
must be in the set of characters defined by the class, unless the first |
3787 |
character in the class definition is a circumflex, in which case the |
character in the class definition is a circumflex, in which case the |
3788 |
subject character must not be in the set defined by the class. If a |
subject character must not be in the set defined by the class. If a |
3789 |
circumflex is actually required as a member of the class, ensure it is |
circumflex is actually required as a member of the class, ensure it is |
3790 |
not the first character, or escape it with a backslash. |
not the first character, or escape it with a backslash. |
3791 |
|
|
3792 |
For example, the character class [aeiou] matches any lower case vowel, |
For example, the character class [aeiou] matches any lower case vowel, |
3793 |
while [^aeiou] matches any character that is not a lower case vowel. |
while [^aeiou] matches any character that is not a lower case vowel. |
3794 |
Note that a circumflex is just a convenient notation for specifying the |
Note that a circumflex is just a convenient notation for specifying the |
3795 |
characters that are in the class by enumerating those that are not. A |
characters that are in the class by enumerating those that are not. A |
3796 |
class that starts with a circumflex is not an assertion; it still con- |
class that starts with a circumflex is not an assertion; it still con- |
3797 |
sumes a character from the subject string, and therefore it fails if |
sumes a character from the subject string, and therefore it fails if |
3798 |
the current pointer is at the end of the string. |
the current pointer is at the end of the string. |
3799 |
|
|
3800 |
In UTF-8 mode, characters with values greater than 255 can be included |
In UTF-8 mode, characters with values greater than 255 can be included |
3801 |
in a class as a literal string of bytes, or by using the \x{ escaping |
in a class as a literal string of bytes, or by using the \x{ escaping |
3802 |
mechanism. |
mechanism. |
3803 |
|
|
3804 |
When caseless matching is set, any letters in a class represent both |
When caseless matching is set, any letters in a class represent both |
3805 |
their upper case and lower case versions, so for example, a caseless |
their upper case and lower case versions, so for example, a caseless |
3806 |
[aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not |
[aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not |
3807 |
match "A", whereas a caseful version would. In UTF-8 mode, PCRE always |
match "A", whereas a caseful version would. In UTF-8 mode, PCRE always |
3808 |
understands the concept of case for characters whose values are less |
understands the concept of case for characters whose values are less |
3809 |
than 128, so caseless matching is always possible. For characters with |
than 128, so caseless matching is always possible. For characters with |
3810 |
higher values, the concept of case is supported if PCRE is compiled |
higher values, the concept of case is supported if PCRE is compiled |
3811 |
with Unicode property support, but not otherwise. If you want to use |
with Unicode property support, but not otherwise. If you want to use |
3812 |
caseless matching in UTF8-mode for characters 128 and above, you must |
caseless matching in UTF8-mode for characters 128 and above, you must |
3813 |
ensure that PCRE is compiled with Unicode property support as well as |
ensure that PCRE is compiled with Unicode property support as well as |
3814 |
with UTF-8 support. |
with UTF-8 support. |
3815 |
|
|
3816 |
Characters that might indicate line breaks are never treated in any |
Characters that might indicate line breaks are never treated in any |
3817 |
special way when matching character classes, whatever line-ending |
special way when matching character classes, whatever line-ending |
3818 |
sequence is in use, and whatever setting of the PCRE_DOTALL and |
sequence is in use, and whatever setting of the PCRE_DOTALL and |
3819 |
PCRE_MULTILINE options is used. A class such as [^a] always matches one |
PCRE_MULTILINE options is used. A class such as [^a] always matches one |
3820 |
of these characters. |
of these characters. |
3821 |
|
|
3822 |
The minus (hyphen) character can be used to specify a range of charac- |
The minus (hyphen) character can be used to specify a range of charac- |
3823 |
ters in a character class. For example, [d-m] matches any letter |
ters in a character class. For example, [d-m] matches any letter |
3824 |
between d and m, inclusive. If a minus character is required in a |
between d and m, inclusive. If a minus character is required in a |
3825 |
class, it must be escaped with a backslash or appear in a position |
class, it must be escaped with a backslash or appear in a position |
3826 |
where it cannot be interpreted as indicating a range, typically as the |
where it cannot be interpreted as indicating a range, typically as the |
3827 |
first or last character in the class. |
first or last character in the class. |
3828 |
|
|
3829 |
It is not possible to have the literal character "]" as the end charac- |
It is not possible to have the literal character "]" as the end charac- |
3830 |
ter of a range. A pattern such as [W-]46] is interpreted as a class of |
ter of a range. A pattern such as [W-]46] is interpreted as a class of |
3831 |
two characters ("W" and "-") followed by a literal string "46]", so it |
two characters ("W" and "-") followed by a literal string "46]", so it |
3832 |
would match "W46]" or "-46]". However, if the "]" is escaped with a |
would match "W46]" or "-46]". However, if the "]" is escaped with a |
3833 |
backslash it is interpreted as the end of range, so [W-\]46] is inter- |
backslash it is interpreted as the end of range, so [W-\]46] is inter- |
3834 |
preted as a class containing a range followed by two other characters. |
preted as a class containing a range followed by two other characters. |
3835 |
The octal or hexadecimal representation of "]" can also be used to end |
The octal or hexadecimal representation of "]" can also be used to end |
3836 |
a range. |
a range. |
3837 |
|
|
3838 |
Ranges operate in the collating sequence of character values. They can |
Ranges operate in the collating sequence of character values. They can |
3839 |
also be used for characters specified numerically, for example |
also be used for characters specified numerically, for example |
3840 |
[\000-\037]. In UTF-8 mode, ranges can include characters whose values |
[\000-\037]. In UTF-8 mode, ranges can include characters whose values |
3841 |
are greater than 255, for example [\x{100}-\x{2ff}]. |
are greater than 255, for example [\x{100}-\x{2ff}]. |
3842 |
|
|
3843 |
If a range that includes letters is used when caseless matching is set, |
If a range that includes letters is used when caseless matching is set, |
3844 |
it matches the letters in either case. For example, [W-c] is equivalent |
it matches the letters in either case. For example, [W-c] is equivalent |
3845 |
to [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if |
to [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if |
3846 |
character tables for a French locale are in use, [\xc8-\xcb] matches |
character tables for a French locale are in use, [\xc8-\xcb] matches |
3847 |
accented E characters in both cases. In UTF-8 mode, PCRE supports the |
accented E characters in both cases. In UTF-8 mode, PCRE supports the |
3848 |
concept of case for characters with values greater than 128 only when |
concept of case for characters with values greater than 128 only when |
3849 |
it is compiled with Unicode property support. |
it is compiled with Unicode property support. |
3850 |
|
|
3851 |
The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear |
The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear |
3852 |
in a character class, and add the characters that they match to the |
in a character class, and add the characters that they match to the |
3853 |
class. For example, [\dABCDEF] matches any hexadecimal digit. A circum- |
class. For example, [\dABCDEF] matches any hexadecimal digit. A circum- |
3854 |
flex can conveniently be used with the upper case character types to |
flex can conveniently be used with the upper case character types to |
3855 |
specify a more restricted set of characters than the matching lower |
specify a more restricted set of characters than the matching lower |
3856 |
case type. For example, the class [^\W_] matches any letter or digit, |
case type. For example, the class [^\W_] matches any letter or digit, |
3857 |
but not underscore. |
but not underscore. |
3858 |
|
|
3859 |
The only metacharacters that are recognized in character classes are |
The only metacharacters that are recognized in character classes are |
3860 |
backslash, hyphen (only where it can be interpreted as specifying a |
backslash, hyphen (only where it can be interpreted as specifying a |
3861 |
range), circumflex (only at the start), opening square bracket (only |
range), circumflex (only at the start), opening square bracket (only |
3862 |
when it can be interpreted as introducing a POSIX class name - see the |
when it can be interpreted as introducing a POSIX class name - see the |
3863 |
next section), and the terminating closing square bracket. However, |
next section), and the terminating closing square bracket. However, |
3864 |
escaping other non-alphanumeric characters does no harm. |
escaping other non-alphanumeric characters does no harm. |
3865 |
|
|
3866 |
|
|
3867 |
POSIX CHARACTER CLASSES |
POSIX CHARACTER CLASSES |
3868 |
|
|
3869 |
Perl supports the POSIX notation for character classes. This uses names |
Perl supports the POSIX notation for character classes. This uses names |
3870 |
enclosed by [: and :] within the enclosing square brackets. PCRE also |
enclosed by [: and :] within the enclosing square brackets. PCRE also |
3871 |
supports this notation. For example, |
supports this notation. For example, |
3872 |
|
|
3873 |
[01[:alpha:]%] |
[01[:alpha:]%] |
3890 |
word "word" characters (same as \w) |
word "word" characters (same as \w) |
3891 |
xdigit hexadecimal digits |
xdigit hexadecimal digits |
3892 |
|
|
3893 |
The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), |
The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), |
3894 |
and space (32). Notice that this list includes the VT character (code |
and space (32). Notice that this list includes the VT character (code |
3895 |
11). This makes "space" different to \s, which does not include VT (for |
11). This makes "space" different to \s, which does not include VT (for |
3896 |
Perl compatibility). |
Perl compatibility). |
3897 |
|
|
3898 |
The name "word" is a Perl extension, and "blank" is a GNU extension |
The name "word" is a Perl extension, and "blank" is a GNU extension |
3899 |
from Perl 5.8. Another Perl extension is negation, which is indicated |
from Perl 5.8. Another Perl extension is negation, which is indicated |
3900 |
by a ^ character after the colon. For example, |
by a ^ character after the colon. For example, |
3901 |
|
|
3902 |
[12[:^digit:]] |
[12[:^digit:]] |
3903 |
|
|
3904 |
matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the |
matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the |
3905 |
POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but |
POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but |
3906 |
these are not supported, and an error is given if they are encountered. |
these are not supported, and an error is given if they are encountered. |
3907 |
|
|
3911 |
|
|
3912 |
VERTICAL BAR |
VERTICAL BAR |
3913 |
|
|
3914 |
Vertical bar characters are used to separate alternative patterns. For |
Vertical bar characters are used to separate alternative patterns. For |
3915 |
example, the pattern |
example, the pattern |
3916 |
|
|
3917 |
gilbert|sullivan |
gilbert|sullivan |
3918 |
|
|
3919 |
matches either "gilbert" or "sullivan". Any number of alternatives may |
matches either "gilbert" or "sullivan". Any number of alternatives may |
3920 |
appear, and an empty alternative is permitted (matching the empty |
appear, and an empty alternative is permitted (matching the empty |
3921 |
string). The matching process tries each alternative in turn, from left |
string). The matching process tries each alternative in turn, from left |
3922 |
to right, and the first one that succeeds is used. If the alternatives |
to right, and the first one that succeeds is used. If the alternatives |
3923 |
are within a subpattern (defined below), "succeeds" means matching the |
are within a subpattern (defined below), "succeeds" means matching the |
3924 |
rest of the main pattern as well as the alternative in the subpattern. |
rest of the main pattern as well as the alternative in the subpattern. |
3925 |
|
|
3926 |
|
|
3927 |
INTERNAL OPTION SETTING |
INTERNAL OPTION SETTING |
3928 |
|
|
3929 |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
3930 |
PCRE_EXTENDED options (which are Perl-compatible) can be changed from |
PCRE_EXTENDED options (which are Perl-compatible) can be changed from |
3931 |
within the pattern by a sequence of Perl option letters enclosed |
within the pattern by a sequence of Perl option letters enclosed |
3932 |
between "(?" and ")". The option letters are |
between "(?" and ")". The option letters are |
3933 |
|
|
3934 |
i for PCRE_CASELESS |
i for PCRE_CASELESS |
3938 |
|
|
3939 |
For example, (?im) sets caseless, multiline matching. It is also possi- |
For example, (?im) sets caseless, multiline matching. It is also possi- |
3940 |
ble to unset these options by preceding the letter with a hyphen, and a |
ble to unset these options by preceding the letter with a hyphen, and a |
3941 |
combined setting and unsetting such as (?im-sx), which sets PCRE_CASE- |
combined setting and unsetting such as (?im-sx), which sets PCRE_CASE- |
3942 |
LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, |
LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, |
3943 |
is also permitted. If a letter appears both before and after the |
is also permitted. If a letter appears both before and after the |
3944 |
hyphen, the option is unset. |
hyphen, the option is unset. |
3945 |
|
|
3946 |
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA |
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA |
3947 |
can be changed in the same way as the Perl-compatible options by using |
can be changed in the same way as the Perl-compatible options by using |
3948 |
the characters J, U and X respectively. |
the characters J, U and X respectively. |
3949 |
|
|
3950 |
When one of these option changes occurs at top level (that is, not |
When one of these option changes occurs at top level (that is, not |
3951 |
inside subpattern parentheses), the change applies to the remainder of |
inside subpattern parentheses), the change applies to the remainder of |
3952 |
the pattern that follows. If the change is placed right at the start of |
the pattern that follows. If the change is placed right at the start of |
3953 |
a pattern, PCRE extracts it into the global options (and it will there- |
a pattern, PCRE extracts it into the global options (and it will there- |
3954 |
fore show up in data extracted by the pcre_fullinfo() function). |
fore show up in data extracted by the pcre_fullinfo() function). |
3955 |
|
|
3956 |
An option change within a subpattern (see below for a description of |
An option change within a subpattern (see below for a description of |
3957 |
subpatterns) affects only that part of the current pattern that follows |
subpatterns) affects only that part of the current pattern that follows |
3958 |
it, so |
it, so |
3959 |
|
|
3960 |
(a(?i)b)c |
(a(?i)b)c |
3961 |
|
|
3962 |
matches abc and aBc and no other strings (assuming PCRE_CASELESS is not |
matches abc and aBc and no other strings (assuming PCRE_CASELESS is not |
3963 |
used). By this means, options can be made to have different settings |
used). By this means, options can be made to have different settings |
3964 |
in different parts of the pattern. Any changes made in one alternative |
in different parts of the pattern. Any changes made in one alternative |
3965 |
do carry on into subsequent branches within the same subpattern. For |
do carry on into subsequent branches within the same subpattern. For |
3966 |
example, |
example, |
3967 |
|
|
3968 |
(a(?i)b|c) |
(a(?i)b|c) |
3969 |
|
|
3970 |
matches "ab", "aB", "c", and "C", even though when matching "C" the |
matches "ab", "aB", "c", and "C", even though when matching "C" the |
3971 |
first branch is abandoned before the option setting. This is because |
first branch is abandoned before the option setting. This is because |
3972 |
the effects of option settings happen at compile time. There would be |
the effects of option settings happen at compile time. There would be |
3973 |
some very weird behaviour otherwise. |
some very weird behaviour otherwise. |
3974 |
|
|
3975 |
Note: There are other PCRE-specific options that can be set by the |
Note: There are other PCRE-specific options that can be set by the |
3976 |
application when the compile or match functions are called. In some |
application when the compile or match functions are called. In some |
3977 |
cases the pattern can contain special leading sequences such as (*CRLF) |
cases the pattern can contain special leading sequences such as (*CRLF) |
3978 |
to override what the application has set or what has been defaulted. |
to override what the application has set or what has been defaulted. |
3979 |
Details are given in the section entitled "Newline sequences" above. |
Details are given in the section entitled "Newline sequences" above. |
3980 |
There is also the (*UTF8) leading sequence that can be used to set |
There is also the (*UTF8) leading sequence that can be used to set |
3981 |
UTF-8 mode; this is equivalent to setting the PCRE_UTF8 option. |
UTF-8 mode; this is equivalent to setting the PCRE_UTF8 option. |
3982 |
|
|
3983 |
|
|
3990 |
|
|
3991 |
cat(aract|erpillar|) |
cat(aract|erpillar|) |
3992 |
|
|
3993 |
matches one of the words "cat", "cataract", or "caterpillar". Without |
matches one of the words "cat", "cataract", or "caterpillar". Without |
3994 |
the parentheses, it would match "cataract", "erpillar" or an empty |
the parentheses, it would match "cataract", "erpillar" or an empty |
3995 |
string. |
string. |
3996 |
|
|
3997 |
2. It sets up the subpattern as a capturing subpattern. This means |
2. It sets up the subpattern as a capturing subpattern. This means |
3998 |
that, when the whole pattern matches, that portion of the subject |
that, when the whole pattern matches, that portion of the subject |
3999 |
string that matched the subpattern is passed back to the caller via the |
string that matched the subpattern is passed back to the caller via the |
4000 |
ovector argument of pcre_exec(). Opening parentheses are counted from |
ovector argument of pcre_exec(). Opening parentheses are counted from |
4001 |
left to right (starting from 1) to obtain numbers for the capturing |
left to right (starting from 1) to obtain numbers for the capturing |
4002 |
subpatterns. |
subpatterns. |
4003 |
|
|
4004 |
For example, if the string "the red king" is matched against the pat- |
For example, if the string "the red king" is matched against the pat- |
4005 |
tern |
tern |
4006 |
|
|
4007 |
the ((red|white) (king|queen)) |
the ((red|white) (king|queen)) |
4009 |
the captured substrings are "red king", "red", and "king", and are num- |
the captured substrings are "red king", "red", and "king", and are num- |
4010 |
bered 1, 2, and 3, respectively. |
bered 1, 2, and 3, respectively. |
4011 |
|
|
4012 |
The fact that plain parentheses fulfil two functions is not always |
The fact that plain parentheses fulfil two functions is not always |
4013 |
helpful. There are often times when a grouping subpattern is required |
helpful. There are often times when a grouping subpattern is required |
4014 |
without a capturing requirement. If an opening parenthesis is followed |
without a capturing requirement. If an opening parenthesis is followed |
4015 |
by a question mark and a colon, the subpattern does not do any captur- |
by a question mark and a colon, the subpattern does not do any captur- |
4016 |
ing, and is not counted when computing the number of any subsequent |
ing, and is not counted when computing the number of any subsequent |
4017 |
capturing subpatterns. For example, if the string "the white queen" is |
capturing subpatterns. For example, if the string "the white queen" is |
4018 |
matched against the pattern |
matched against the pattern |
4019 |
|
|
4020 |
the ((?:red|white) (king|queen)) |
the ((?:red|white) (king|queen)) |
4022 |
the captured substrings are "white queen" and "queen", and are numbered |
the captured substrings are "white queen" and "queen", and are numbered |
4023 |
1 and 2. The maximum number of capturing subpatterns is 65535. |
1 and 2. The maximum number of capturing subpatterns is 65535. |
4024 |
|
|
4025 |
As a convenient shorthand, if any option settings are required at the |
As a convenient shorthand, if any option settings are required at the |
4026 |
start of a non-capturing subpattern, the option letters may appear |
start of a non-capturing subpattern, the option letters may appear |
4027 |
between the "?" and the ":". Thus the two patterns |
between the "?" and the ":". Thus the two patterns |
4028 |
|
|
4029 |
(?i:saturday|sunday) |
(?i:saturday|sunday) |
4030 |
(?:(?i)saturday|sunday) |
(?:(?i)saturday|sunday) |
4031 |
|
|
4032 |
match exactly the same set of strings. Because alternative branches are |
match exactly the same set of strings. Because alternative branches are |
4033 |
tried from left to right, and options are not reset until the end of |
tried from left to right, and options are not reset until the end of |
4034 |
the subpattern is reached, an option setting in one branch does affect |
the subpattern is reached, an option setting in one branch does affect |
4035 |
subsequent branches, so the above patterns match "SUNDAY" as well as |
subsequent branches, so the above patterns match "SUNDAY" as well as |
4036 |
"Saturday". |
"Saturday". |
4037 |
|
|
4038 |
|
|
4039 |
DUPLICATE SUBPATTERN NUMBERS |
DUPLICATE SUBPATTERN NUMBERS |
4040 |
|
|
4041 |
Perl 5.10 introduced a feature whereby each alternative in a subpattern |
Perl 5.10 introduced a feature whereby each alternative in a subpattern |
4042 |
uses the same numbers for its capturing parentheses. Such a subpattern |
uses the same numbers for its capturing parentheses. Such a subpattern |
4043 |
starts with (?| and is itself a non-capturing subpattern. For example, |
starts with (?| and is itself a non-capturing subpattern. For example, |
4044 |
consider this pattern: |
consider this pattern: |
4045 |
|
|
4046 |
(?|(Sat)ur|(Sun))day |
(?|(Sat)ur|(Sun))day |
4047 |
|
|
4048 |
Because the two alternatives are inside a (?| group, both sets of cap- |
Because the two alternatives are inside a (?| group, both sets of cap- |
4049 |
turing parentheses are numbered one. Thus, when the pattern matches, |
turing parentheses are numbered one. Thus, when the pattern matches, |
4050 |
you can look at captured substring number one, whichever alternative |
you can look at captured substring number one, whichever alternative |
4051 |
matched. This construct is useful when you want to capture part, but |
matched. This construct is useful when you want to capture part, but |
4052 |
not all, of one of a number of alternatives. Inside a (?| group, paren- |
not all, of one of a number of alternatives. Inside a (?| group, paren- |
4053 |
theses are numbered as usual, but the number is reset at the start of |
theses are numbered as usual, but the number is reset at the start of |
4054 |
each branch. The numbers of any capturing buffers that follow the sub- |
each branch. The numbers of any capturing buffers that follow the sub- |
4055 |
pattern start after the highest number used in any branch. The follow- |
pattern start after the highest number used in any branch. The follow- |
4056 |
ing example is taken from the Perl documentation. The numbers under- |
ing example is taken from the Perl documentation. The numbers under- |
4057 |
neath show in which buffer the captured content will be stored. |
neath show in which buffer the captured content will be stored. |
4058 |
|
|
4059 |
# before ---------------branch-reset----------- after |
# before ---------------branch-reset----------- after |
4060 |
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
4061 |
# 1 2 2 3 2 3 4 |
# 1 2 2 3 2 3 4 |
4062 |
|
|
4063 |
A back reference to a numbered subpattern uses the most recent value |
A back reference to a numbered subpattern uses the most recent value |
4064 |
that is set for that number by any subpattern. The following pattern |
that is set for that number by any subpattern. The following pattern |
4065 |
matches "abcabc" or "defdef": |
matches "abcabc" or "defdef": |
4066 |
|
|
4067 |
/(?|(abc)|(def))\1/ |
/(?|(abc)|(def))\1/ |
4068 |
|
|
4069 |
In contrast, a recursive or "subroutine" call to a numbered subpattern |
In contrast, a recursive or "subroutine" call to a numbered subpattern |
4070 |
always refers to the first one in the pattern with the given number. |
always refers to the first one in the pattern with the given number. |
4071 |
The following pattern matches "abcabc" or "defabc": |
The following pattern matches "abcabc" or "defabc": |
4072 |
|
|
4073 |
/(?|(abc)|(def))(?1)/ |
/(?|(abc)|(def))(?1)/ |
4074 |
|
|
4075 |
If a condition test for a subpattern's having matched refers to a non- |
If a condition test for a subpattern's having matched refers to a non- |
4076 |
unique number, the test is true if any of the subpatterns of that num- |
unique number, the test is true if any of the subpatterns of that num- |
4077 |
ber have matched. |
ber have matched. |
4078 |
|
|
4079 |
An alternative approach to using this "branch reset" feature is to use |
An alternative approach to using this "branch reset" feature is to use |
4080 |
duplicate named subpatterns, as described in the next section. |
duplicate named subpatterns, as described in the next section. |
4081 |
|
|
4082 |
|
|
4083 |
NAMED SUBPATTERNS |
NAMED SUBPATTERNS |
4084 |
|
|
4085 |
Identifying capturing parentheses by number is simple, but it can be |
Identifying capturing parentheses by number is simple, but it can be |
4086 |
very hard to keep track of the numbers in complicated regular expres- |
very hard to keep track of the numbers in complicated regular expres- |
4087 |
sions. Furthermore, if an expression is modified, the numbers may |
sions. Furthermore, if an expression is modified, the numbers may |
4088 |
change. To help with this difficulty, PCRE supports the naming of sub- |
change. To help with this difficulty, PCRE supports the naming of sub- |
4089 |
patterns. This feature was not added to Perl until release 5.10. Python |
patterns. This feature was not added to Perl until release 5.10. Python |
4090 |
had the feature earlier, and PCRE introduced it at release 4.0, using |
had the feature earlier, and PCRE introduced it at release 4.0, using |
4091 |
the Python syntax. PCRE now supports both the Perl and the Python syn- |
the Python syntax. PCRE now supports both the Perl and the Python syn- |
4092 |
tax. Perl allows identically numbered subpatterns to have different |
tax. Perl allows identically numbered subpatterns to have different |
4093 |
names, but PCRE does not. |
names, but PCRE does not. |
4094 |
|
|
4095 |
In PCRE, a subpattern can be named in one of three ways: (?<name>...) |
In PCRE, a subpattern can be named in one of three ways: (?<name>...) |
4096 |
or (?'name'...) as in Perl, or (?P<name>...) as in Python. References |
or (?'name'...) as in Perl, or (?P<name>...) as in Python. References |
4097 |
to capturing parentheses from other parts of the pattern, such as back |
to capturing parentheses from other parts of the pattern, such as back |
4098 |
references, recursion, and conditions, can be made by name as well as |
references, recursion, and conditions, can be made by name as well as |
4099 |
by number. |
by number. |
4100 |
|
|
4101 |
Names consist of up to 32 alphanumeric characters and underscores. |
Names consist of up to 32 alphanumeric characters and underscores. |
4102 |
Named capturing parentheses are still allocated numbers as well as |
Named capturing parentheses are still allocated numbers as well as |
4103 |
names, exactly as if the names were not present. The PCRE API provides |
names, exactly as if the names were not present. The PCRE API provides |
4104 |
function calls for extracting the name-to-number translation table from |
function calls for extracting the name-to-number translation table from |
4105 |
a compiled pattern. There is also a convenience function for extracting |
a compiled pattern. There is also a convenience function for extracting |
4106 |
a captured substring by name. |
a captured substring by name. |
4107 |
|
|
4108 |
By default, a name must be unique within a pattern, but it is possible |
By default, a name must be unique within a pattern, but it is possible |
4109 |
to relax this constraint by setting the PCRE_DUPNAMES option at compile |
to relax this constraint by setting the PCRE_DUPNAMES option at compile |
4110 |
time. (Duplicate names are also always permitted for subpatterns with |
time. (Duplicate names are also always permitted for subpatterns with |
4111 |
the same number, set up as described in the previous section.) Dupli- |
the same number, set up as described in the previous section.) Dupli- |
4112 |
cate names can be useful for patterns where only one instance of the |
cate names can be useful for patterns where only one instance of the |
4113 |
named parentheses can match. Suppose you want to match the name of a |
named parentheses can match. Suppose you want to match the name of a |
4114 |
weekday, either as a 3-letter abbreviation or as the full name, and in |
weekday, either as a 3-letter abbreviation or as the full name, and in |
4115 |
both cases you want to extract the abbreviation. This pattern (ignoring |
both cases you want to extract the abbreviation. This pattern (ignoring |
4116 |
the line breaks) does the job: |
the line breaks) does the job: |
4117 |
|
|
4121 |
(?<DN>Thu)(?:rsday)?| |
(?<DN>Thu)(?:rsday)?| |
4122 |
(?<DN>Sat)(?:urday)? |
(?<DN>Sat)(?:urday)? |
4123 |
|
|
4124 |
There are five capturing substrings, but only one is ever set after a |
There are five capturing substrings, but only one is ever set after a |
4125 |
match. (An alternative way of solving this problem is to use a "branch |
match. (An alternative way of solving this problem is to use a "branch |
4126 |
reset" subpattern, as described in the previous section.) |
reset" subpattern, as described in the previous section.) |
4127 |
|
|
4128 |
The convenience function for extracting the data by name returns the |
The convenience function for extracting the data by name returns the |
4129 |
substring for the first (and in this example, the only) subpattern of |
substring for the first (and in this example, the only) subpattern of |
4130 |
that name that matched. This saves searching to find which numbered |
that name that matched. This saves searching to find which numbered |
4131 |
subpattern it was. |
subpattern it was. |
4132 |
|
|
4133 |
If you make a back reference to a non-unique named subpattern from |
If you make a back reference to a non-unique named subpattern from |
4134 |
elsewhere in the pattern, the one that corresponds to the first occur- |
elsewhere in the pattern, the one that corresponds to the first occur- |
4135 |
rence of the name is used. In the absence of duplicate numbers (see the |
rence of the name is used. In the absence of duplicate numbers (see the |
4136 |
previous section) this is the one with the lowest number. If you use a |
previous section) this is the one with the lowest number. If you use a |
4137 |
named reference in a condition test (see the section about conditions |
named reference in a condition test (see the section about conditions |
4138 |
below), either to check whether a subpattern has matched, or to check |
below), either to check whether a subpattern has matched, or to check |
4139 |
for recursion, all subpatterns with the same name are tested. If the |
for recursion, all subpatterns with the same name are tested. If the |
4140 |
condition is true for any one of them, the overall condition is true. |
condition is true for any one of them, the overall condition is true. |
4141 |
This is the same behaviour as testing by number. For further details of |
This is the same behaviour as testing by number. For further details of |
4142 |
the interfaces for handling named subpatterns, see the pcreapi documen- |
the interfaces for handling named subpatterns, see the pcreapi documen- |
4143 |
tation. |
tation. |
4144 |
|
|
4145 |
Warning: You cannot use different names to distinguish between two sub- |
Warning: You cannot use different names to distinguish between two sub- |
4146 |
patterns with the same number because PCRE uses only the numbers when |
patterns with the same number because PCRE uses only the numbers when |
4147 |
matching. For this reason, an error is given at compile time if differ- |
matching. For this reason, an error is given at compile time if differ- |
4148 |
ent names are given to subpatterns with the same number. However, you |
ent names are given to subpatterns with the same number. However, you |
4149 |
can give the same name to subpatterns with the same number, even when |
can give the same name to subpatterns with the same number, even when |
4150 |
PCRE_DUPNAMES is not set. |
PCRE_DUPNAMES is not set. |
4151 |
|
|
4152 |
|
|
4153 |
REPETITION |
REPETITION |
4154 |
|
|
4155 |
Repetition is specified by quantifiers, which can follow any of the |
Repetition is specified by quantifiers, which can follow any of the |
4156 |
following items: |
following items: |
4157 |
|
|
4158 |
a literal data character |
a literal data character |
4166 |
a parenthesized subpattern (unless it is an assertion) |
a parenthesized subpattern (unless it is an assertion) |
4167 |
a recursive or "subroutine" call to a subpattern |
a recursive or "subroutine" call to a subpattern |
4168 |
|
|
4169 |
The general repetition quantifier specifies a minimum and maximum num- |
The general repetition quantifier specifies a minimum and maximum num- |
4170 |
ber of permitted matches, by giving the two numbers in curly brackets |
ber of permitted matches, by giving the two numbers in curly brackets |
4171 |
(braces), separated by a comma. The numbers must be less than 65536, |
(braces), separated by a comma. The numbers must be less than 65536, |
4172 |
and the first must be less than or equal to the second. For example: |
and the first must be less than or equal to the second. For example: |
4173 |
|
|
4174 |
z{2,4} |
z{2,4} |
4175 |
|
|
4176 |
matches "zz", "zzz", or "zzzz". A closing brace on its own is not a |
matches "zz", "zzz", or "zzzz". A closing brace on its own is not a |
4177 |
special character. If the second number is omitted, but the comma is |
special character. If the second number is omitted, but the comma is |
4178 |
present, there is no upper limit; if the second number and the comma |
present, there is no upper limit; if the second number and the comma |
4179 |
are both omitted, the quantifier specifies an exact number of required |
are both omitted, the quantifier specifies an exact number of required |
4180 |
matches. Thus |
matches. Thus |
4181 |
|
|
4182 |
[aeiou]{3,} |
[aeiou]{3,} |
4185 |
|
|
4186 |
\d{8} |
\d{8} |
4187 |
|
|
4188 |
matches exactly 8 digits. An opening curly bracket that appears in a |
matches exactly 8 digits. An opening curly bracket that appears in a |
4189 |
position where a quantifier is not allowed, or one that does not match |
position where a quantifier is not allowed, or one that does not match |
4190 |
the syntax of a quantifier, is taken as a literal character. For exam- |
the syntax of a quantifier, is taken as a literal character. For exam- |
4191 |
ple, {,6} is not a quantifier, but a literal string of four characters. |
ple, {,6} is not a quantifier, but a literal string of four characters. |
4192 |
|
|
4193 |
In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to |
In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to |
4194 |
individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char- |
individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char- |
4195 |
acters, each of which is represented by a two-byte sequence. Similarly, |
acters, each of which is represented by a two-byte sequence. Similarly, |
4196 |
when Unicode property support is available, \X{3} matches three Unicode |
when Unicode property support is available, \X{3} matches three Unicode |
4197 |
extended sequences, each of which may be several bytes long (and they |
extended sequences, each of which may be several bytes long (and they |
4198 |
may be of different lengths). |
may be of different lengths). |
4199 |
|
|
4200 |
The quantifier {0} is permitted, causing the expression to behave as if |
The quantifier {0} is permitted, causing the expression to behave as if |
4201 |
the previous item and the quantifier were not present. This may be use- |
the previous item and the quantifier were not present. This may be use- |
4202 |
ful for subpatterns that are referenced as subroutines from elsewhere |
ful for subpatterns that are referenced as subroutines from elsewhere |
4203 |
in the pattern. Items other than subpatterns that have a {0} quantifier |
in the pattern. Items other than subpatterns that have a {0} quantifier |
4204 |
are omitted from the compiled pattern. |
are omitted from the compiled pattern. |
4205 |
|
|
4206 |
For convenience, the three most common quantifiers have single-charac- |
For convenience, the three most common quantifiers have single-charac- |
4207 |
ter abbreviations: |
ter abbreviations: |
4208 |
|
|
4209 |
* is equivalent to {0,} |
* is equivalent to {0,} |
4210 |
+ is equivalent to {1,} |
+ is equivalent to {1,} |
4211 |
? is equivalent to {0,1} |
? is equivalent to {0,1} |
4212 |
|
|
4213 |
It is possible to construct infinite loops by following a subpattern |
It is possible to construct infinite loops by following a subpattern |
4214 |
that can match no characters with a quantifier that has no upper limit, |
that can match no characters with a quantifier that has no upper limit, |
4215 |
for example: |
for example: |
4216 |
|
|
4217 |
(a?)* |
(a?)* |
4218 |
|
|
4219 |
Earlier versions of Perl and PCRE used to give an error at compile time |
Earlier versions of Perl and PCRE used to give an error at compile time |
4220 |
for such patterns. However, because there are cases where this can be |
for such patterns. However, because there are cases where this can be |
4221 |
useful, such patterns are now accepted, but if any repetition of the |
useful, such patterns are now accepted, but if any repetition of the |
4222 |
subpattern does in fact match no characters, the loop is forcibly bro- |
subpattern does in fact match no characters, the loop is forcibly bro- |
4223 |
ken. |
ken. |
4224 |
|
|
4225 |
By default, the quantifiers are "greedy", that is, they match as much |
By default, the quantifiers are "greedy", that is, they match as much |
4226 |
as possible (up to the maximum number of permitted times), without |
as possible (up to the maximum number of permitted times), without |
4227 |
causing the rest of the pattern to fail. The classic example of where |
causing the rest of the pattern to fail. The classic example of where |
4228 |
this gives problems is in trying to match comments in C programs. These |
this gives problems is in trying to match comments in C programs. These |
4229 |
appear between /* and */ and within the comment, individual * and / |
appear between /* and */ and within the comment, individual * and / |
4230 |
characters may appear. An attempt to match C comments by applying the |
characters may appear. An attempt to match C comments by applying the |
4231 |
pattern |
pattern |
4232 |
|
|
4233 |
/\*.*\*/ |
/\*.*\*/ |
4236 |
|
|
4237 |
/* first comment */ not comment /* second comment */ |
/* first comment */ not comment /* second comment */ |
4238 |
|
|
4239 |
fails, because it matches the entire string owing to the greediness of |
fails, because it matches the entire string owing to the greediness of |
4240 |
the .* item. |
the .* item. |
4241 |
|
|
4242 |
However, if a quantifier is followed by a question mark, it ceases to |
However, if a quantifier is followed by a question mark, it ceases to |
4243 |
be greedy, and instead matches the minimum number of times possible, so |
be greedy, and instead matches the minimum number of times possible, so |
4244 |
the pattern |
the pattern |
4245 |
|
|
4246 |
/\*.*?\*/ |
/\*.*?\*/ |
4247 |
|
|
4248 |
does the right thing with the C comments. The meaning of the various |
does the right thing with the C comments. The meaning of the various |
4249 |
quantifiers is not otherwise changed, just the preferred number of |
quantifiers is not otherwise changed, just the preferred number of |
4250 |
matches. Do not confuse this use of question mark with its use as a |
matches. Do not confuse this use of question mark with its use as a |
4251 |
quantifier in its own right. Because it has two uses, it can sometimes |
quantifier in its own right. Because it has two uses, it can sometimes |
4252 |
appear doubled, as in |
appear doubled, as in |
4253 |
|
|
4254 |
\d??\d |
\d??\d |
4256 |
which matches one digit by preference, but can match two if that is the |
which matches one digit by preference, but can match two if that is the |
4257 |
only way the rest of the pattern matches. |
only way the rest of the pattern matches. |
4258 |
|
|
4259 |
If the PCRE_UNGREEDY option is set (an option that is not available in |
If the PCRE_UNGREEDY option is set (an option that is not available in |
4260 |
Perl), the quantifiers are not greedy by default, but individual ones |
Perl), the quantifiers are not greedy by default, but individual ones |
4261 |
can be made greedy by following them with a question mark. In other |
can be made greedy by following them with a question mark. In other |
4262 |
words, it inverts the default behaviour. |
words, it inverts the default behaviour. |
4263 |
|
|
4264 |
When a parenthesized subpattern is quantified with a minimum repeat |
When a parenthesized subpattern is quantified with a minimum repeat |
4265 |
count that is greater than 1 or with a limited maximum, more memory is |
count that is greater than 1 or with a limited maximum, more memory is |
4266 |
required for the compiled pattern, in proportion to the size of the |
required for the compiled pattern, in proportion to the size of the |
4267 |
minimum or maximum. |
minimum or maximum. |
4268 |
|
|
4269 |
If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv- |
If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv- |
4270 |
alent to Perl's /s) is set, thus allowing the dot to match newlines, |
alent to Perl's /s) is set, thus allowing the dot to match newlines, |
4271 |
the pattern is implicitly anchored, because whatever follows will be |
the pattern is implicitly anchored, because whatever follows will be |
4272 |
tried against every character position in the subject string, so there |
tried against every character position in the subject string, so there |
4273 |
is no point in retrying the overall match at any position after the |
is no point in retrying the overall match at any position after the |
4274 |
first. PCRE normally treats such a pattern as though it were preceded |
first. PCRE normally treats such a pattern as though it were preceded |
4275 |
by \A. |
by \A. |
4276 |
|
|
4277 |
In cases where it is known that the subject string contains no new- |
In cases where it is known that the subject string contains no new- |
4278 |
lines, it is worth setting PCRE_DOTALL in order to obtain this opti- |
lines, it is worth setting PCRE_DOTALL in order to obtain this opti- |
4279 |
mization, or alternatively using ^ to indicate anchoring explicitly. |
mization, or alternatively using ^ to indicate anchoring explicitly. |
4280 |
|
|
4281 |
However, there is one situation where the optimization cannot be used. |
However, there is one situation where the optimization cannot be used. |
4282 |
When .* is inside capturing parentheses that are the subject of a back |
When .* is inside capturing parentheses that are the subject of a back |
4283 |
reference elsewhere in the pattern, a match at the start may fail where |
reference elsewhere in the pattern, a match at the start may fail where |
4284 |
a later one succeeds. Consider, for example: |
a later one succeeds. Consider, for example: |
4285 |
|
|
4286 |
(.*)abc\1 |
(.*)abc\1 |
4287 |
|
|
4288 |
If the subject is "xyz123abc123" the match point is the fourth charac- |
If the subject is "xyz123abc123" the match point is the fourth charac- |
4289 |
ter. For this reason, such a pattern is not implicitly anchored. |
ter. For this reason, such a pattern is not implicitly anchored. |
4290 |
|
|
4291 |
When a capturing subpattern is repeated, the value captured is the sub- |
When a capturing subpattern is repeated, the value captured is the sub- |
4294 |
(tweedle[dume]{3}\s*)+ |
(tweedle[dume]{3}\s*)+ |
4295 |
|
|
4296 |
has matched "tweedledum tweedledee" the value of the captured substring |
has matched "tweedledum tweedledee" the value of the captured substring |
4297 |
is "tweedledee". However, if there are nested capturing subpatterns, |
is "tweedledee". However, if there are nested capturing subpatterns, |
4298 |
the corresponding captured values may have been set in previous itera- |
the corresponding captured values may have been set in previous itera- |
4299 |
tions. For example, after |
tions. For example, after |
4300 |
|
|
4301 |
/(a|(b))+/ |
/(a|(b))+/ |
4305 |
|
|
4306 |
ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS |
ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS |
4307 |
|
|
4308 |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
4309 |
repetition, failure of what follows normally causes the repeated item |
repetition, failure of what follows normally causes the repeated item |
4310 |
to be re-evaluated to see if a different number of repeats allows the |
to be re-evaluated to see if a different number of repeats allows the |
4311 |
rest of the pattern to match. Sometimes it is useful to prevent this, |
rest of the pattern to match. Sometimes it is useful to prevent this, |
4312 |
either to change the nature of the match, or to cause it fail earlier |
either to change the nature of the match, or to cause it fail earlier |
4313 |
than it otherwise might, when the author of the pattern knows there is |
than it otherwise might, when the author of the pattern knows there is |
4314 |
no point in carrying on. |
no point in carrying on. |
4315 |
|
|
4316 |
Consider, for example, the pattern \d+foo when applied to the subject |
Consider, for example, the pattern \d+foo when applied to the subject |
4317 |
line |
line |
4318 |
|
|
4319 |
123456bar |
123456bar |
4320 |
|
|
4321 |
After matching all 6 digits and then failing to match "foo", the normal |
After matching all 6 digits and then failing to match "foo", the normal |
4322 |
action of the matcher is to try again with only 5 digits matching the |
action of the matcher is to try again with only 5 digits matching the |
4323 |
\d+ item, and then with 4, and so on, before ultimately failing. |
\d+ item, and then with 4, and so on, before ultimately failing. |
4324 |
"Atomic grouping" (a term taken from Jeffrey Friedl's book) provides |
"Atomic grouping" (a term taken from Jeffrey Friedl's book) provides |
4325 |
the means for specifying that once a subpattern has matched, it is not |
the means for specifying that once a subpattern has matched, it is not |
4326 |
to be re-evaluated in this way. |
to be re-evaluated in this way. |
4327 |
|
|
4328 |
If we use atomic grouping for the previous example, the matcher gives |
If we use atomic grouping for the previous example, the matcher gives |
4329 |
up immediately on failing to match "foo" the first time. The notation |
up immediately on failing to match "foo" the first time. The notation |
4330 |
is a kind of special parenthesis, starting with (?> as in this example: |
is a kind of special parenthesis, starting with (?> as in this example: |
4331 |
|
|
4332 |
(?>\d+)foo |
(?>\d+)foo |
4333 |
|
|
4334 |
This kind of parenthesis "locks up" the part of the pattern it con- |
This kind of parenthesis "locks up" the part of the pattern it con- |
4335 |
tains once it has matched, and a failure further into the pattern is |
tains once it has matched, and a failure further into the pattern is |
4336 |
prevented from backtracking into it. Backtracking past it to previous |
prevented from backtracking into it. Backtracking past it to previous |
4337 |
items, however, works as normal. |
items, however, works as normal. |
4338 |
|
|
4339 |
An alternative description is that a subpattern of this type matches |
An alternative description is that a subpattern of this type matches |
4340 |
the string of characters that an identical standalone pattern would |
the string of characters that an identical standalone pattern would |
4341 |
match, if anchored at the current point in the subject string. |
match, if anchored at the current point in the subject string. |
4342 |
|
|
4343 |
Atomic grouping subpatterns are not capturing subpatterns. Simple cases |
Atomic grouping subpatterns are not capturing subpatterns. Simple cases |
4344 |
such as the above example can be thought of as a maximizing repeat that |
such as the above example can be thought of as a maximizing repeat that |
4345 |
must swallow everything it can. So, while both \d+ and \d+? are pre- |
must swallow everything it can. So, while both \d+ and \d+? are pre- |
4346 |
pared to adjust the number of digits they match in order to make the |
pared to adjust the number of digits they match in order to make the |
4347 |
rest of the pattern match, (?>\d+) can only match an entire sequence of |
rest of the pattern match, (?>\d+) can only match an entire sequence of |
4348 |
digits. |
digits. |
4349 |
|
|
4350 |
Atomic groups in general can of course contain arbitrarily complicated |
Atomic groups in general can of course contain arbitrarily complicated |
4351 |
subpatterns, and can be nested. However, when the subpattern for an |
subpatterns, and can be nested. However, when the subpattern for an |
4352 |
atomic group is just a single repeated item, as in the example above, a |
atomic group is just a single repeated item, as in the example above, a |
4353 |
simpler notation, called a "possessive quantifier" can be used. This |
simpler notation, called a "possessive quantifier" can be used. This |
4354 |
consists of an additional + character following a quantifier. Using |
consists of an additional + character following a quantifier. Using |
4355 |
this notation, the previous example can be rewritten as |
this notation, the previous example can be rewritten as |
4356 |
|
|
4357 |
\d++foo |
\d++foo |
4361 |
|
|
4362 |
(abc|xyz){2,3}+ |
(abc|xyz){2,3}+ |
4363 |
|
|
4364 |
Possessive quantifiers are always greedy; the setting of the |
Possessive quantifiers are always greedy; the setting of the |
4365 |
PCRE_UNGREEDY option is ignored. They are a convenient notation for the |
PCRE_UNGREEDY option is ignored. They are a convenient notation for the |
4366 |
simpler forms of atomic group. However, there is no difference in the |
simpler forms of atomic group. However, there is no difference in the |
4367 |
meaning of a possessive quantifier and the equivalent atomic group, |
meaning of a possessive quantifier and the equivalent atomic group, |
4368 |
though there may be a performance difference; possessive quantifiers |
though there may be a performance difference; possessive quantifiers |
4369 |
should be slightly faster. |
should be slightly faster. |
4370 |
|
|
4371 |
The possessive quantifier syntax is an extension to the Perl 5.8 syn- |
The possessive quantifier syntax is an extension to the Perl 5.8 syn- |
4372 |
tax. Jeffrey Friedl originated the idea (and the name) in the first |
tax. Jeffrey Friedl originated the idea (and the name) in the first |
4373 |
edition of his book. Mike McCloskey liked it, so implemented it when he |
edition of his book. Mike McCloskey liked it, so implemented it when he |
4374 |
built Sun's Java package, and PCRE copied it from there. It ultimately |
built Sun's Java package, and PCRE copied it from there. It ultimately |
4375 |
found its way into Perl at release 5.10. |
found its way into Perl at release 5.10. |
4376 |
|
|
4377 |
PCRE has an optimization that automatically "possessifies" certain sim- |
PCRE has an optimization that automatically "possessifies" certain sim- |
4378 |
ple pattern constructs. For example, the sequence A+B is treated as |
ple pattern constructs. For example, the sequence A+B is treated as |
4379 |
A++B because there is no point in backtracking into a sequence of A's |
A++B because there is no point in backtracking into a sequence of A's |
4380 |
when B must follow. |
when B must follow. |
4381 |
|
|
4382 |
When a pattern contains an unlimited repeat inside a subpattern that |
When a pattern contains an unlimited repeat inside a subpattern that |
4383 |
can itself be repeated an unlimited number of times, the use of an |
can itself be repeated an unlimited number of times, the use of an |
4384 |
atomic group is the only way to avoid some failing matches taking a |
atomic group is the only way to avoid some failing matches taking a |
4385 |
very long time indeed. The pattern |
very long time indeed. The pattern |
4386 |
|
|
4387 |
(\D+|<\d+>)*[!?] |
(\D+|<\d+>)*[!?] |
4388 |
|
|
4389 |
matches an unlimited number of substrings that either consist of non- |
matches an unlimited number of substrings that either consist of non- |
4390 |
digits, or digits enclosed in <>, followed by either ! or ?. When it |
digits, or digits enclosed in <>, followed by either ! or ?. When it |
4391 |
matches, it runs quickly. However, if it is applied to |
matches, it runs quickly. However, if it is applied to |
4392 |
|
|
4393 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
4394 |
|
|
4395 |
it takes a long time before reporting failure. This is because the |
it takes a long time before reporting failure. This is because the |
4396 |
string can be divided between the internal \D+ repeat and the external |
string can be divided between the internal \D+ repeat and the external |
4397 |
* repeat in a large number of ways, and all have to be tried. (The |
* repeat in a large number of ways, and all have to be tried. (The |
4398 |
example uses [!?] rather than a single character at the end, because |
example uses [!?] rather than a single character at the end, because |
4399 |
both PCRE and Perl have an optimization that allows for fast failure |
both PCRE and Perl have an optimization that allows for fast failure |
4400 |
when a single character is used. They remember the last single charac- |
when a single character is used. They remember the last single charac- |
4401 |
ter that is required for a match, and fail early if it is not present |
ter that is required for a match, and fail early if it is not present |
4402 |
in the string.) If the pattern is changed so that it uses an atomic |
in the string.) If the pattern is changed so that it uses an atomic |
4403 |
group, like this: |
group, like this: |
4404 |
|
|
4405 |
((?>\D+)|<\d+>)*[!?] |
((?>\D+)|<\d+>)*[!?] |
4411 |
|
|
4412 |
Outside a character class, a backslash followed by a digit greater than |
Outside a character class, a backslash followed by a digit greater than |
4413 |
0 (and possibly further digits) is a back reference to a capturing sub- |
0 (and possibly further digits) is a back reference to a capturing sub- |
4414 |
pattern earlier (that is, to its left) in the pattern, provided there |
pattern earlier (that is, to its left) in the pattern, provided there |
4415 |
have been that many previous capturing left parentheses. |
have been that many previous capturing left parentheses. |
4416 |
|
|
4417 |
However, if the decimal number following the backslash is less than 10, |
However, if the decimal number following the backslash is less than 10, |
4418 |
it is always taken as a back reference, and causes an error only if |
it is always taken as a back reference, and causes an error only if |
4419 |
there are not that many capturing left parentheses in the entire pat- |
there are not that many capturing left parentheses in the entire pat- |
4420 |
tern. In other words, the parentheses that are referenced need not be |
tern. In other words, the parentheses that are referenced need not be |
4421 |
to the left of the reference for numbers less than 10. A "forward back |
to the left of the reference for numbers less than 10. A "forward back |
4422 |
reference" of this type can make sense when a repetition is involved |
reference" of this type can make sense when a repetition is involved |
4423 |
and the subpattern to the right has participated in an earlier itera- |
and the subpattern to the right has participated in an earlier itera- |
4424 |
tion. |
tion. |
4425 |
|
|
4426 |
It is not possible to have a numerical "forward back reference" to a |
It is not possible to have a numerical "forward back reference" to a |
4427 |
subpattern whose number is 10 or more using this syntax because a |
subpattern whose number is 10 or more using this syntax because a |
4428 |
sequence such as \50 is interpreted as a character defined in octal. |
sequence such as \50 is interpreted as a character defined in octal. |
4429 |
See the subsection entitled "Non-printing characters" above for further |
See the subsection entitled "Non-printing characters" above for further |
4430 |
details of the handling of digits following a backslash. There is no |
details of the handling of digits following a backslash. There is no |
4431 |
such problem when named parentheses are used. A back reference to any |
such problem when named parentheses are used. A back reference to any |
4432 |
subpattern is possible using named parentheses (see below). |
subpattern is possible using named parentheses (see below). |
4433 |
|
|
4434 |
Another way of avoiding the ambiguity inherent in the use of digits |
Another way of avoiding the ambiguity inherent in the use of digits |
4435 |
following a backslash is to use the \g escape sequence, which is a fea- |
following a backslash is to use the \g escape sequence, which is a fea- |
4436 |
ture introduced in Perl 5.10. This escape must be followed by an |
ture introduced in Perl 5.10. This escape must be followed by an |
4437 |
unsigned number or a negative number, optionally enclosed in braces. |
unsigned number or a negative number, optionally enclosed in braces. |
4438 |
These examples are all identical: |
These examples are all identical: |
4439 |
|
|
4440 |
(ring), \1 |
(ring), \1 |
4441 |
(ring), \g1 |
(ring), \g1 |
4442 |
(ring), \g{1} |
(ring), \g{1} |
4443 |
|
|
4444 |
An unsigned number specifies an absolute reference without the ambigu- |
An unsigned number specifies an absolute reference without the ambigu- |
4445 |
ity that is present in the older syntax. It is also useful when literal |
ity that is present in the older syntax. It is also useful when literal |
4446 |
digits follow the reference. A negative number is a relative reference. |
digits follow the reference. A negative number is a relative reference. |
4447 |
Consider this example: |
Consider this example: |
4449 |
(abc(def)ghi)\g{-1} |
(abc(def)ghi)\g{-1} |
4450 |
|
|
4451 |
The sequence \g{-1} is a reference to the most recently started captur- |
The sequence \g{-1} is a reference to the most recently started captur- |
4452 |
ing subpattern before \g, that is, is it equivalent to \2. Similarly, |
ing subpattern before \g, that is, is it equivalent to \2. Similarly, |
4453 |
\g{-2} would be equivalent to \1. The use of relative references can be |
\g{-2} would be equivalent to \1. The use of relative references can be |
4454 |
helpful in long patterns, and also in patterns that are created by |
helpful in long patterns, and also in patterns that are created by |
4455 |
joining together fragments that contain references within themselves. |
joining together fragments that contain references within themselves. |
4456 |
|
|
4457 |
A back reference matches whatever actually matched the capturing sub- |
A back reference matches whatever actually matched the capturing sub- |
4458 |
pattern in the current subject string, rather than anything matching |
pattern in the current subject string, rather than anything matching |
4459 |
the subpattern itself (see "Subpatterns as subroutines" below for a way |
the subpattern itself (see "Subpatterns as subroutines" below for a way |
4460 |
of doing that). So the pattern |
of doing that). So the pattern |
4461 |
|
|
4462 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
4463 |
|
|
4464 |
matches "sense and sensibility" and "response and responsibility", but |
matches "sense and sensibility" and "response and responsibility", but |
4465 |
not "sense and responsibility". If caseful matching is in force at the |
not "sense and responsibility". If caseful matching is in force at the |
4466 |
time of the back reference, the case of letters is relevant. For exam- |
time of the back reference, the case of letters is relevant. For exam- |
4467 |
ple, |
ple, |
4468 |
|
|
4469 |
((?i)rah)\s+\1 |
((?i)rah)\s+\1 |
4470 |
|
|
4471 |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the |
4472 |
original capturing subpattern is matched caselessly. |
original capturing subpattern is matched caselessly. |
4473 |
|
|
4474 |
There are several different ways of writing back references to named |
There are several different ways of writing back references to named |
4475 |
subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or |
subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or |
4476 |
\k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's |
\k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's |
4477 |
unified back reference syntax, in which \g can be used for both numeric |
unified back reference syntax, in which \g can be used for both numeric |
4478 |
and named references, is also supported. We could rewrite the above |
and named references, is also supported. We could rewrite the above |
4479 |
example in any of the following ways: |
example in any of the following ways: |
4480 |
|
|
4481 |
(?<p1>(?i)rah)\s+\k<p1> |
(?<p1>(?i)rah)\s+\k<p1> |
4483 |
(?P<p1>(?i)rah)\s+(?P=p1) |
(?P<p1>(?i)rah)\s+(?P=p1) |
4484 |
(?<p1>(?i)rah)\s+\g{p1} |
(?<p1>(?i)rah)\s+\g{p1} |
4485 |
|
|
4486 |
A subpattern that is referenced by name may appear in the pattern |
A subpattern that is referenced by name may appear in the pattern |
4487 |
before or after the reference. |
before or after the reference. |
4488 |
|
|
4489 |
There may be more than one back reference to the same subpattern. If a |
There may be more than one back reference to the same subpattern. If a |
4490 |
subpattern has not actually been used in a particular match, any back |
subpattern has not actually been used in a particular match, any back |
4491 |
references to it always fail by default. For example, the pattern |
references to it always fail by default. For example, the pattern |
4492 |
|
|
4493 |
(a|(bc))\2 |
(a|(bc))\2 |
4494 |
|
|
4495 |
always fails if it starts to match "a" rather than "bc". However, if |
always fails if it starts to match "a" rather than "bc". However, if |
4496 |
the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer- |
the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer- |
4497 |
ence to an unset value matches an empty string. |
ence to an unset value matches an empty string. |
4498 |
|
|
4499 |
Because there may be many capturing parentheses in a pattern, all dig- |
Because there may be many capturing parentheses in a pattern, all dig- |
4500 |
its following a backslash are taken as part of a potential back refer- |
its following a backslash are taken as part of a potential back refer- |
4501 |
ence number. If the pattern continues with a digit character, some |
ence number. If the pattern continues with a digit character, some |
4502 |
delimiter must be used to terminate the back reference. If the |
delimiter must be used to terminate the back reference. If the |
4503 |
PCRE_EXTENDED option is set, this can be whitespace. Otherwise, the \g{ |
PCRE_EXTENDED option is set, this can be whitespace. Otherwise, the \g{ |
4504 |
syntax or an empty comment (see "Comments" below) can be used. |
syntax or an empty comment (see "Comments" below) can be used. |
4505 |
|
|
4506 |
Recursive back references |
Recursive back references |
4507 |
|
|
4508 |
A back reference that occurs inside the parentheses to which it refers |
A back reference that occurs inside the parentheses to which it refers |
4509 |
fails when the subpattern is first used, so, for example, (a\1) never |
fails when the subpattern is first used, so, for example, (a\1) never |
4510 |
matches. However, such references can be useful inside repeated sub- |
matches. However, such references can be useful inside repeated sub- |
4511 |
patterns. For example, the pattern |
patterns. For example, the pattern |
4512 |
|
|
4513 |
(a|b\1)+ |
(a|b\1)+ |
4514 |
|
|
4515 |
matches any number of "a"s and also "aba", "ababbaa" etc. At each iter- |
matches any number of "a"s and also "aba", "ababbaa" etc. At each iter- |
4516 |
ation of the subpattern, the back reference matches the character |
ation of the subpattern, the back reference matches the character |
4517 |
string corresponding to the previous iteration. In order for this to |
string corresponding to the previous iteration. In order for this to |
4518 |
work, the pattern must be such that the first iteration does not need |
work, the pattern must be such that the first iteration does not need |
4519 |
to match the back reference. This can be done using alternation, as in |
to match the back reference. This can be done using alternation, as in |
4520 |
the example above, or by a quantifier with a minimum of zero. |
the example above, or by a quantifier with a minimum of zero. |
4521 |
|
|
4522 |
Back references of this type cause the group that they reference to be |
Back references of this type cause the group that they reference to be |
4523 |
treated as an atomic group. Once the whole group has been matched, a |
treated as an atomic group. Once the whole group has been matched, a |
4524 |
subsequent matching failure cannot cause backtracking into the middle |
subsequent matching failure cannot cause backtracking into the middle |
4525 |
of the group. |
of the group. |
4526 |
|
|
4527 |
|
|
4528 |
ASSERTIONS |
ASSERTIONS |
4529 |
|
|
4530 |
An assertion is a test on the characters following or preceding the |
An assertion is a test on the characters following or preceding the |
4531 |
current matching point that does not actually consume any characters. |
current matching point that does not actually consume any characters. |
4532 |
The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are |
The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are |
4533 |
described above. |
described above. |
4534 |
|
|
4535 |
More complicated assertions are coded as subpatterns. There are two |
More complicated assertions are coded as subpatterns. There are two |
4536 |
kinds: those that look ahead of the current position in the subject |
kinds: those that look ahead of the current position in the subject |
4537 |
string, and those that look behind it. An assertion subpattern is |
string, and those that look behind it. An assertion subpattern is |
4538 |
matched in the normal way, except that it does not cause the current |
matched in the normal way, except that it does not cause the current |
4539 |
matching position to be changed. |
matching position to be changed. |
4540 |
|
|
4541 |
Assertion subpatterns are not capturing subpatterns, and may not be |
Assertion subpatterns are not capturing subpatterns, and may not be |
4542 |
repeated, because it makes no sense to assert the same thing several |
repeated, because it makes no sense to assert the same thing several |
4543 |
times. If any kind of assertion contains capturing subpatterns within |
times. If any kind of assertion contains capturing subpatterns within |
4544 |
it, these are counted for the purposes of numbering the capturing sub- |
it, these are counted for the purposes of numbering the capturing sub- |
4545 |
patterns in the whole pattern. However, substring capturing is carried |
patterns in the whole pattern. However, substring capturing is carried |
4546 |
out only for positive assertions, because it does not make sense for |
out only for positive assertions, because it does not make sense for |
4547 |
negative assertions. |
negative assertions. |
4548 |
|
|
4549 |
Lookahead assertions |
Lookahead assertions |
4553 |
|
|
4554 |
\w+(?=;) |
\w+(?=;) |
4555 |
|
|
4556 |
matches a word followed by a semicolon, but does not include the semi- |
matches a word followed by a semicolon, but does not include the semi- |
4557 |
colon in the match, and |
colon in the match, and |
4558 |
|
|
4559 |
foo(?!bar) |
foo(?!bar) |
4560 |
|
|
4561 |
matches any occurrence of "foo" that is not followed by "bar". Note |
matches any occurrence of "foo" that is not followed by "bar". Note |
4562 |
that the apparently similar pattern |
that the apparently similar pattern |
4563 |
|
|
4564 |
(?!foo)bar |
(?!foo)bar |
4565 |
|
|
4566 |
does not find an occurrence of "bar" that is preceded by something |
does not find an occurrence of "bar" that is preceded by something |
4567 |
other than "foo"; it finds any occurrence of "bar" whatsoever, because |
other than "foo"; it finds any occurrence of "bar" whatsoever, because |
4568 |
the assertion (?!foo) is always true when the next three characters are |
the assertion (?!foo) is always true when the next three characters are |
4569 |
"bar". A lookbehind assertion is needed to achieve the other effect. |
"bar". A lookbehind assertion is needed to achieve the other effect. |
4570 |
|
|
4571 |
If you want to force a matching failure at some point in a pattern, the |
If you want to force a matching failure at some point in a pattern, the |
4572 |
most convenient way to do it is with (?!) because an empty string |
most convenient way to do it is with (?!) because an empty string |
4573 |
always matches, so an assertion that requires there not to be an empty |
always matches, so an assertion that requires there not to be an empty |
4574 |
string must always fail. The Perl 5.10 backtracking control verb |
string must always fail. The Perl 5.10 backtracking control verb |
4575 |
(*FAIL) or (*F) is essentially a synonym for (?!). |
(*FAIL) or (*F) is essentially a synonym for (?!). |
4576 |
|
|
4577 |
Lookbehind assertions |
Lookbehind assertions |
4578 |
|
|
4579 |
Lookbehind assertions start with (?<= for positive assertions and (?<! |
Lookbehind assertions start with (?<= for positive assertions and (?<! |
4580 |
for negative assertions. For example, |
for negative assertions. For example, |
4581 |
|
|
4582 |
(?<!foo)bar |
(?<!foo)bar |
4583 |
|
|
4584 |
does find an occurrence of "bar" that is not preceded by "foo". The |
does find an occurrence of "bar" that is not preceded by "foo". The |
4585 |
contents of a lookbehind assertion are restricted such that all the |
contents of a lookbehind assertion are restricted such that all the |
4586 |
strings it matches must have a fixed length. However, if there are sev- |
strings it matches must have a fixed length. However, if there are sev- |
4587 |
eral top-level alternatives, they do not all have to have the same |
eral top-level alternatives, they do not all have to have the same |
4588 |
fixed length. Thus |
fixed length. Thus |
4589 |
|
|
4590 |
(?<=bullock|donkey) |
(?<=bullock|donkey) |
4593 |
|
|
4594 |
(?<!dogs?|cats?) |
(?<!dogs?|cats?) |
4595 |
|
|
4596 |
causes an error at compile time. Branches that match different length |
causes an error at compile time. Branches that match different length |
4597 |
strings are permitted only at the top level of a lookbehind assertion. |
strings are permitted only at the top level of a lookbehind assertion. |
4598 |
This is an extension compared with Perl (5.8 and 5.10), which requires |
This is an extension compared with Perl (5.8 and 5.10), which requires |
4599 |
all branches to match the same length of string. An assertion such as |
all branches to match the same length of string. An assertion such as |
4600 |
|
|
4601 |
(?<=ab(c|de)) |
(?<=ab(c|de)) |
4602 |
|
|
4603 |
is not permitted, because its single top-level branch can match two |
is not permitted, because its single top-level branch can match two |
4604 |
different lengths, but it is acceptable to PCRE if rewritten to use two |
different lengths, but it is acceptable to PCRE if rewritten to use two |
4605 |
top-level branches: |
top-level branches: |
4606 |
|
|
4607 |
(?<=abc|abde) |
(?<=abc|abde) |
4608 |
|
|
4609 |
In some cases, the Perl 5.10 escape sequence \K (see above) can be used |
In some cases, the Perl 5.10 escape sequence \K (see above) can be used |
4610 |
instead of a lookbehind assertion to get round the fixed-length |
instead of a lookbehind assertion to get round the fixed-length |
4611 |
restriction. |
restriction. |
4612 |
|
|
4613 |
The implementation of lookbehind assertions is, for each alternative, |
The implementation of lookbehind assertions is, for each alternative, |
4614 |
to temporarily move the current position back by the fixed length and |
to temporarily move the current position back by the fixed length and |
4615 |
then try to match. If there are insufficient characters before the cur- |
then try to match. If there are insufficient characters before the cur- |
4616 |
rent position, the assertion fails. |
rent position, the assertion fails. |
4617 |
|
|
4618 |
PCRE does not allow the \C escape (which matches a single byte in UTF-8 |
PCRE does not allow the \C escape (which matches a single byte in UTF-8 |
4619 |
mode) to appear in lookbehind assertions, because it makes it impossi- |
mode) to appear in lookbehind assertions, because it makes it impossi- |
4620 |
ble to calculate the length of the lookbehind. The \X and \R escapes, |
ble to calculate the length of the lookbehind. The \X and \R escapes, |
4621 |
which can match different numbers of bytes, are also not permitted. |
which can match different numbers of bytes, are also not permitted. |
4622 |
|
|
4623 |
"Subroutine" calls (see below) such as (?2) or (?&X) are permitted in |
"Subroutine" calls (see below) such as (?2) or (?&X) are permitted in |
4624 |
lookbehinds, as long as the subpattern matches a fixed-length string. |
lookbehinds, as long as the subpattern matches a fixed-length string. |
4625 |
Recursion, however, is not supported. |
Recursion, however, is not supported. |
4626 |
|
|
4627 |
Possessive quantifiers can be used in conjunction with lookbehind |
Possessive quantifiers can be used in conjunction with lookbehind |
4628 |
assertions to specify efficient matching of fixed-length strings at the |
assertions to specify efficient matching of fixed-length strings at the |
4629 |
end of subject strings. Consider a simple pattern such as |
end of subject strings. Consider a simple pattern such as |
4630 |
|
|
4631 |
abcd$ |
abcd$ |
4632 |
|
|
4633 |
when applied to a long string that does not match. Because matching |
when applied to a long string that does not match. Because matching |
4634 |
proceeds from left to right, PCRE will look for each "a" in the subject |
proceeds from left to right, PCRE will look for each "a" in the subject |
4635 |
and then see if what follows matches the rest of the pattern. If the |
and then see if what follows matches the rest of the pattern. If the |
4636 |
pattern is specified as |
pattern is specified as |
4637 |
|
|
4638 |
^.*abcd$ |
^.*abcd$ |
4639 |
|
|
4640 |
the initial .* matches the entire string at first, but when this fails |
the initial .* matches the entire string at first, but when this fails |
4641 |
(because there is no following "a"), it backtracks to match all but the |
(because there is no following "a"), it backtracks to match all but the |
4642 |
last character, then all but the last two characters, and so on. Once |
last character, then all but the last two characters, and so on. Once |
4643 |
again the search for "a" covers the entire string, from right to left, |
again the search for "a" covers the entire string, from right to left, |
4644 |
so we are no better off. However, if the pattern is written as |
so we are no better off. However, if the pattern is written as |
4645 |
|
|
4646 |
^.*+(?<=abcd) |
^.*+(?<=abcd) |
4647 |
|
|
4648 |
there can be no backtracking for the .*+ item; it can match only the |
there can be no backtracking for the .*+ item; it can match only the |
4649 |
entire string. The subsequent lookbehind assertion does a single test |
entire string. The subsequent lookbehind assertion does a single test |
4650 |
on the last four characters. If it fails, the match fails immediately. |
on the last four characters. If it fails, the match fails immediately. |
4651 |
For long strings, this approach makes a significant difference to the |
For long strings, this approach makes a significant difference to the |
4652 |
processing time. |
processing time. |
4653 |
|
|
4654 |
Using multiple assertions |
Using multiple assertions |
4657 |
|
|
4658 |
(?<=\d{3})(?<!999)foo |
(?<=\d{3})(?<!999)foo |
4659 |
|
|
4660 |
matches "foo" preceded by three digits that are not "999". Notice that |
matches "foo" preceded by three digits that are not "999". Notice that |
4661 |
each of the assertions is applied independently at the same point in |
each of the assertions is applied independently at the same point in |
4662 |
the subject string. First there is a check that the previous three |
the subject string. First there is a check that the previous three |
4663 |
characters are all digits, and then there is a check that the same |
characters are all digits, and then there is a check that the same |
4664 |
three characters are not "999". This pattern does not match "foo" pre- |
three characters are not "999". This pattern does not match "foo" pre- |
4665 |
ceded by six characters, the first of which are digits and the last |
ceded by six characters, the first of which are digits and the last |
4666 |
three of which are not "999". For example, it doesn't match "123abc- |
three of which are not "999". For example, it doesn't match "123abc- |
4667 |
foo". A pattern to do that is |
foo". A pattern to do that is |
4668 |
|
|
4669 |
(?<=\d{3}...)(?<!999)foo |
(?<=\d{3}...)(?<!999)foo |
4670 |
|
|
4671 |
This time the first assertion looks at the preceding six characters, |
This time the first assertion looks at the preceding six characters, |
4672 |
checking that the first three are digits, and then the second assertion |
checking that the first three are digits, and then the second assertion |
4673 |
checks that the preceding three characters are not "999". |
checks that the preceding three characters are not "999". |
4674 |
|
|
4676 |
|
|
4677 |
(?<=(?<!foo)bar)baz |
(?<=(?<!foo)bar)baz |
4678 |
|
|
4679 |
matches an occurrence of "baz" that is preceded by "bar" which in turn |
matches an occurrence of "baz" that is preceded by "bar" which in turn |
4680 |
is not preceded by "foo", while |
is not preceded by "foo", while |
4681 |
|
|
4682 |
(?<=\d{3}(?!999)...)foo |
(?<=\d{3}(?!999)...)foo |
4683 |
|
|
4684 |
is another pattern that matches "foo" preceded by three digits and any |
is another pattern that matches "foo" preceded by three digits and any |
4685 |
three characters that are not "999". |
three characters that are not "999". |
4686 |
|
|
4687 |
|
|
4688 |
CONDITIONAL SUBPATTERNS |
CONDITIONAL SUBPATTERNS |
4689 |
|
|
4690 |
It is possible to cause the matching process to obey a subpattern con- |
It is possible to cause the matching process to obey a subpattern con- |
4691 |
ditionally or to choose between two alternative subpatterns, depending |
ditionally or to choose between two alternative subpatterns, depending |
4692 |
on the result of an assertion, or whether a specific capturing subpat- |
on the result of an assertion, or whether a specific capturing subpat- |
4693 |
tern has already been matched. The two possible forms of conditional |
tern has already been matched. The two possible forms of conditional |
4694 |
subpattern are: |
subpattern are: |
4695 |
|
|
4696 |
(?(condition)yes-pattern) |
(?(condition)yes-pattern) |
4697 |
(?(condition)yes-pattern|no-pattern) |
(?(condition)yes-pattern|no-pattern) |
4698 |
|
|
4699 |
If the condition is satisfied, the yes-pattern is used; otherwise the |
If the condition is satisfied, the yes-pattern is used; otherwise the |
4700 |
no-pattern (if present) is used. If there are more than two alterna- |
no-pattern (if present) is used. If there are more than two alterna- |
4701 |
tives in the subpattern, a compile-time error occurs. |
tives in the subpattern, a compile-time error occurs. |
4702 |
|
|
4703 |
There are four kinds of condition: references to subpatterns, refer- |
There are four kinds of condition: references to subpatterns, refer- |
4704 |
ences to recursion, a pseudo-condition called DEFINE, and assertions. |
ences to recursion, a pseudo-condition called DEFINE, and assertions. |
4705 |
|
|
4706 |
Checking for a used subpattern by number |
Checking for a used subpattern by number |
4707 |
|
|
4708 |
If the text between the parentheses consists of a sequence of digits, |
If the text between the parentheses consists of a sequence of digits, |
4709 |
the condition is true if a capturing subpattern of that number has pre- |
the condition is true if a capturing subpattern of that number has pre- |
4710 |
viously matched. If there is more than one capturing subpattern with |
viously matched. If there is more than one capturing subpattern with |
4711 |
the same number (see the earlier section about duplicate subpattern |
the same number (see the earlier section about duplicate subpattern |
4712 |
numbers), the condition is true if any of them have been set. An alter- |
numbers), the condition is true if any of them have been set. An alter- |
4713 |
native notation is to precede the digits with a plus or minus sign. In |
native notation is to precede the digits with a plus or minus sign. In |
4714 |
this case, the subpattern number is relative rather than absolute. The |
this case, the subpattern number is relative rather than absolute. The |
4715 |
most recently opened parentheses can be referenced by (?(-1), the next |
most recently opened parentheses can be referenced by (?(-1), the next |
4716 |
most recent by (?(-2), and so on. In looping constructs it can also |
most recent by (?(-2), and so on. In looping constructs it can also |
4717 |
make sense to refer to subsequent groups with constructs such as |
make sense to refer to subsequent groups with constructs such as |
4718 |
(?(+2). |
(?(+2). |
4719 |
|
|
4720 |
Consider the following pattern, which contains non-significant white |
Consider the following pattern, which contains non-significant white |
4721 |
space to make it more readable (assume the PCRE_EXTENDED option) and to |
space to make it more readable (assume the PCRE_EXTENDED option) and to |
4722 |
divide it into three parts for ease of discussion: |
divide it into three parts for ease of discussion: |
4723 |
|
|
4724 |
( \( )? [^()]+ (?(1) \) ) |
( \( )? [^()]+ (?(1) \) ) |
4725 |
|
|
4726 |
The first part matches an optional opening parenthesis, and if that |
The first part matches an optional opening parenthesis, and if that |
4727 |
character is present, sets it as the first captured substring. The sec- |
character is present, sets it as the first captured substring. The sec- |
4728 |
ond part matches one or more characters that are not parentheses. The |
ond part matches one or more characters that are not parentheses. The |
4729 |
third part is a conditional subpattern that tests whether the first set |
third part is a conditional subpattern that tests whether the first set |
4730 |
of parentheses matched or not. If they did, that is, if subject started |
of parentheses matched or not. If they did, that is, if subject started |
4731 |
with an opening parenthesis, the condition is true, and so the yes-pat- |
with an opening parenthesis, the condition is true, and so the yes-pat- |
4732 |
tern is executed and a closing parenthesis is required. Otherwise, |
tern is executed and a closing parenthesis is required. Otherwise, |
4733 |
since no-pattern is not present, the subpattern matches nothing. In |
since no-pattern is not present, the subpattern matches nothing. In |
4734 |
other words, this pattern matches a sequence of non-parentheses, |
other words, this pattern matches a sequence of non-parentheses, |
4735 |
optionally enclosed in parentheses. |
optionally enclosed in parentheses. |
4736 |
|
|
4737 |
If you were embedding this pattern in a larger one, you could use a |
If you were embedding this pattern in a larger one, you could use a |
4738 |
relative reference: |
relative reference: |
4739 |
|
|
4740 |
...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
4741 |
|
|
4742 |
This makes the fragment independent of the parentheses in the larger |
This makes the fragment independent of the parentheses in the larger |
4743 |
pattern. |
pattern. |
4744 |
|
|
4745 |
Checking for a used subpattern by name |
Checking for a used subpattern by name |
4746 |
|
|
4747 |
Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a |
Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a |
4748 |
used subpattern by name. For compatibility with earlier versions of |
used subpattern by name. For compatibility with earlier versions of |
4749 |
PCRE, which had this facility before Perl, the syntax (?(name)...) is |
PCRE, which had this facility before Perl, the syntax (?(name)...) is |
4750 |
also recognized. However, there is a possible ambiguity with this syn- |
also recognized. However, there is a possible ambiguity with this syn- |
4751 |
tax, because subpattern names may consist entirely of digits. PCRE |
tax, because subpattern names may consist entirely of digits. PCRE |
4752 |
looks first for a named subpattern; if it cannot find one and the name |
looks first for a named subpattern; if it cannot find one and the name |
4753 |
consists entirely of digits, PCRE looks for a subpattern of that num- |
consists entirely of digits, PCRE looks for a subpattern of that num- |
4754 |
ber, which must be greater than zero. Using subpattern names that con- |
ber, which must be greater than zero. Using subpattern names that con- |
4755 |
sist entirely of digits is not recommended. |
sist entirely of digits is not recommended. |
4756 |
|
|
4757 |
Rewriting the above example to use a named subpattern gives this: |
Rewriting the above example to use a named subpattern gives this: |
4758 |
|
|
4759 |
(?<OPEN> \( )? [^()]+ (?(<OPEN>) \) ) |
(?<OPEN> \( )? [^()]+ (?(<OPEN>) \) ) |
4760 |
|
|
4761 |
If the name used in a condition of this kind is a duplicate, the test |
If the name used in a condition of this kind is a duplicate, the test |
4762 |
is applied to all subpatterns of the same name, and is true if any one |
is applied to all subpatterns of the same name, and is true if any one |
4763 |
of them has matched. |
of them has matched. |
4764 |
|
|
4765 |
Checking for pattern recursion |
Checking for pattern recursion |
4766 |
|
|
4767 |
If the condition is the string (R), and there is no subpattern with the |
If the condition is the string (R), and there is no subpattern with the |
4768 |
name R, the condition is true if a recursive call to the whole pattern |
name R, the condition is true if a recursive call to the whole pattern |
4769 |
or any subpattern has been made. If digits or a name preceded by amper- |
or any subpattern has been made. If digits or a name preceded by amper- |
4770 |
sand follow the letter R, for example: |
sand follow the letter R, for example: |
4771 |
|
|
4773 |
|
|
4774 |
the condition is true if the most recent recursion is into a subpattern |
the condition is true if the most recent recursion is into a subpattern |
4775 |
whose number or name is given. This condition does not check the entire |
whose number or name is given. This condition does not check the entire |
4776 |
recursion stack. If the name used in a condition of this kind is a |
recursion stack. If the name used in a condition of this kind is a |
4777 |
duplicate, the test is applied to all subpatterns of the same name, and |
duplicate, the test is applied to all subpatterns of the same name, and |
4778 |
is true if any one of them is the most recent recursion. |
is true if any one of them is the most recent recursion. |
4779 |
|
|
4780 |
At "top level", all these recursion test conditions are false. The |
At "top level", all these recursion test conditions are false. The |
4781 |
syntax for recursive patterns is described below. |
syntax for recursive patterns is described below. |
4782 |
|
|
4783 |
Defining subpatterns for use by reference only |
Defining subpatterns for use by reference only |
4784 |
|
|
4785 |
If the condition is the string (DEFINE), and there is no subpattern |
If the condition is the string (DEFINE), and there is no subpattern |
4786 |
with the name DEFINE, the condition is always false. In this case, |
with the name DEFINE, the condition is always false. In this case, |
4787 |
there may be only one alternative in the subpattern. It is always |
there may be only one alternative in the subpattern. It is always |
4788 |
skipped if control reaches this point in the pattern; the idea of |
skipped if control reaches this point in the pattern; the idea of |
4789 |
DEFINE is that it can be used to define "subroutines" that can be ref- |
DEFINE is that it can be used to define "subroutines" that can be ref- |
4790 |
erenced from elsewhere. (The use of "subroutines" is described below.) |
erenced from elsewhere. (The use of "subroutines" is described below.) |
4791 |
For example, a pattern to match an IPv4 address could be written like |
For example, a pattern to match an IPv4 address could be written like |
4792 |
this (ignore whitespace and line breaks): |
this (ignore whitespace and line breaks): |
4793 |
|
|
4794 |
(?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) ) |
(?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) ) |
4795 |
\b (?&byte) (\.(?&byte)){3} \b |
\b (?&byte) (\.(?&byte)){3} \b |
4796 |
|
|
4797 |
The first part of the pattern is a DEFINE group inside which a another |
The first part of the pattern is a DEFINE group inside which a another |
4798 |
group named "byte" is defined. This matches an individual component of |
group named "byte" is defined. This matches an individual component of |
4799 |
an IPv4 address (a number less than 256). When matching takes place, |
an IPv4 address (a number less than 256). When matching takes place, |
4800 |
this part of the pattern is skipped because DEFINE acts like a false |
this part of the pattern is skipped because DEFINE acts like a false |
4801 |
condition. The rest of the pattern uses references to the named group |
condition. The rest of the pattern uses references to the named group |
4802 |
to match the four dot-separated components of an IPv4 address, insist- |
to match the four dot-separated components of an IPv4 address, insist- |
4803 |
ing on a word boundary at each end. |
ing on a word boundary at each end. |
4804 |
|
|
4805 |
Assertion conditions |
Assertion conditions |
4806 |
|
|
4807 |
If the condition is not in any of the above formats, it must be an |
If the condition is not in any of the above formats, it must be an |
4808 |
assertion. This may be a positive or negative lookahead or lookbehind |
assertion. This may be a positive or negative lookahead or lookbehind |
4809 |
assertion. Consider this pattern, again containing non-significant |
assertion. Consider this pattern, again containing non-significant |
4810 |
white space, and with the two alternatives on the second line: |
white space, and with the two alternatives on the second line: |
4811 |
|
|
4812 |
(?(?=[^a-z]*[a-z]) |
(?(?=[^a-z]*[a-z]) |
4813 |
\d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} ) |
\d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} ) |
4814 |
|
|
4815 |
The condition is a positive lookahead assertion that matches an |
The condition is a positive lookahead assertion that matches an |
4816 |
optional sequence of non-letters followed by a letter. In other words, |
optional sequence of non-letters followed by a letter. In other words, |
4817 |
it tests for the presence of at least one letter in the subject. If a |
it tests for the presence of at least one letter in the subject. If a |
4818 |
letter is found, the subject is matched against the first alternative; |
letter is found, the subject is matched against the first alternative; |
4819 |
otherwise it is matched against the second. This pattern matches |
otherwise it is matched against the second. This pattern matches |
4820 |
strings in one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are |
strings in one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are |
4821 |
letters and dd are digits. |
letters and dd are digits. |
4822 |
|
|
4823 |
|
|
4824 |
COMMENTS |
COMMENTS |
4825 |
|
|
4826 |
The sequence (?# marks the start of a comment that continues up to the |
The sequence (?# marks the start of a comment that continues up to the |
4827 |
next closing parenthesis. Nested parentheses are not permitted. The |
next closing parenthesis. Nested parentheses are not permitted. The |
4828 |
characters that make up a comment play no part in the pattern matching |
characters that make up a comment play no part in the pattern matching |
4829 |
at all. |
at all. |
4830 |
|
|
4831 |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
4832 |
character class introduces a comment that continues to immediately |
character class introduces a comment that continues to immediately |
4833 |
after the next newline in the pattern. |
after the next newline in the pattern. |
4834 |
|
|
4835 |
|
|
4836 |
RECURSIVE PATTERNS |
RECURSIVE PATTERNS |
4837 |
|
|
4838 |
Consider the problem of matching a string in parentheses, allowing for |
Consider the problem of matching a string in parentheses, allowing for |
4839 |
unlimited nested parentheses. Without the use of recursion, the best |
unlimited nested parentheses. Without the use of recursion, the best |
4840 |
that can be done is to use a pattern that matches up to some fixed |
that can be done is to use a pattern that matches up to some fixed |
4841 |
depth of nesting. It is not possible to handle an arbitrary nesting |
depth of nesting. It is not possible to handle an arbitrary nesting |
4842 |
depth. |
depth. |
4843 |
|
|
4844 |
For some time, Perl has provided a facility that allows regular expres- |
For some time, Perl has provided a facility that allows regular expres- |
4845 |
sions to recurse (amongst other things). It does this by interpolating |
sions to recurse (amongst other things). It does this by interpolating |
4846 |
Perl code in the expression at run time, and the code can refer to the |
Perl code in the expression at run time, and the code can refer to the |
4847 |
expression itself. A Perl pattern using code interpolation to solve the |
expression itself. A Perl pattern using code interpolation to solve the |
4848 |
parentheses problem can be created like this: |
parentheses problem can be created like this: |
4849 |
|
|
4853 |
refers recursively to the pattern in which it appears. |
refers recursively to the pattern in which it appears. |
4854 |
|
|
4855 |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, |
4856 |
it supports special syntax for recursion of the entire pattern, and |
it supports special syntax for recursion of the entire pattern, and |
4857 |
also for individual subpattern recursion. After its introduction in |
also for individual subpattern recursion. After its introduction in |
4858 |
PCRE and Python, this kind of recursion was subsequently introduced |
PCRE and Python, this kind of recursion was subsequently introduced |
4859 |
into Perl at release 5.10. |
into Perl at release 5.10. |
4860 |
|
|
4861 |
A special item that consists of (? followed by a number greater than |
A special item that consists of (? followed by a number greater than |
4862 |
zero and a closing parenthesis is a recursive call of the subpattern of |
zero and a closing parenthesis is a recursive call of the subpattern of |
4863 |
the given number, provided that it occurs inside that subpattern. (If |
the given number, provided that it occurs inside that subpattern. (If |
4864 |
not, it is a "subroutine" call, which is described in the next sec- |
not, it is a "subroutine" call, which is described in the next sec- |
4865 |
tion.) The special item (?R) or (?0) is a recursive call of the entire |
tion.) The special item (?R) or (?0) is a recursive call of the entire |
4866 |
regular expression. |
regular expression. |
4867 |
|
|
4868 |
This PCRE pattern solves the nested parentheses problem (assume the |
This PCRE pattern solves the nested parentheses problem (assume the |
4869 |
PCRE_EXTENDED option is set so that white space is ignored): |
PCRE_EXTENDED option is set so that white space is ignored): |
4870 |
|
|
4871 |
\( ( [^()]++ | (?R) )* \) |
\( ( [^()]++ | (?R) )* \) |
4872 |
|
|
4873 |
First it matches an opening parenthesis. Then it matches any number of |
First it matches an opening parenthesis. Then it matches any number of |
4874 |
substrings which can either be a sequence of non-parentheses, or a |
substrings which can either be a sequence of non-parentheses, or a |
4875 |
recursive match of the pattern itself (that is, a correctly parenthe- |
recursive match of the pattern itself (that is, a correctly parenthe- |
4876 |
sized substring). Finally there is a closing parenthesis. Note the use |
sized substring). Finally there is a closing parenthesis. Note the use |
4877 |
of a possessive quantifier to avoid backtracking into sequences of non- |
of a possessive quantifier to avoid backtracking into sequences of non- |
4878 |
parentheses. |
parentheses. |
4879 |
|
|
4880 |
If this were part of a larger pattern, you would not want to recurse |
If this were part of a larger pattern, you would not want to recurse |
4881 |
the entire pattern, so instead you could use this: |
the entire pattern, so instead you could use this: |
4882 |
|
|
4883 |
( \( ( [^()]++ | (?1) )* \) ) |
( \( ( [^()]++ | (?1) )* \) ) |
4884 |
|
|
4885 |
We have put the pattern into parentheses, and caused the recursion to |
We have put the pattern into parentheses, and caused the recursion to |
4886 |
refer to them instead of the whole pattern. |
refer to them instead of the whole pattern. |
4887 |
|
|
4888 |
In a larger pattern, keeping track of parenthesis numbers can be |
In a larger pattern, keeping track of parenthesis numbers can be |
4889 |
tricky. This is made easier by the use of relative references (a Perl |
tricky. This is made easier by the use of relative references (a Perl |
4890 |
5.10 feature). Instead of (?1) in the pattern above you can write |
5.10 feature). Instead of (?1) in the pattern above you can write |
4891 |
(?-2) to refer to the second most recently opened parentheses preceding |
(?-2) to refer to the second most recently opened parentheses preceding |
4892 |
the recursion. In other words, a negative number counts capturing |
the recursion. In other words, a negative number counts capturing |
4893 |
parentheses leftwards from the point at which it is encountered. |
parentheses leftwards from the point at which it is encountered. |
4894 |
|
|
4895 |
It is also possible to refer to subsequently opened parentheses, by |
It is also possible to refer to subsequently opened parentheses, by |
4896 |
writing references such as (?+2). However, these cannot be recursive |
writing references such as (?+2). However, these cannot be recursive |
4897 |
because the reference is not inside the parentheses that are refer- |
because the reference is not inside the parentheses that are refer- |
4898 |
enced. They are always "subroutine" calls, as described in the next |
enced. They are always "subroutine" calls, as described in the next |
4899 |
section. |
section. |
4900 |
|
|
4901 |
An alternative approach is to use named parentheses instead. The Perl |
An alternative approach is to use named parentheses instead. The Perl |
4902 |
syntax for this is (?&name); PCRE's earlier syntax (?P>name) is also |
syntax for this is (?&name); PCRE's earlier syntax (?P>name) is also |
4903 |
supported. We could rewrite the above example as follows: |
supported. We could rewrite the above example as follows: |
4904 |
|
|
4905 |
(?<pn> \( ( [^()]++ | (?&pn) )* \) ) |
(?<pn> \( ( [^()]++ | (?&pn) )* \) ) |
4906 |
|
|
4907 |
If there is more than one subpattern with the same name, the earliest |
If there is more than one subpattern with the same name, the earliest |
4908 |
one is used. |
one is used. |
4909 |
|
|
4910 |
This particular example pattern that we have been looking at contains |
This particular example pattern that we have been looking at contains |
4911 |
nested unlimited repeats, and so the use of a possessive quantifier for |
nested unlimited repeats, and so the use of a possessive quantifier for |
4912 |
matching strings of non-parentheses is important when applying the pat- |
matching strings of non-parentheses is important when applying the pat- |
4913 |
tern to strings that do not match. For example, when this pattern is |
tern to strings that do not match. For example, when this pattern is |
4914 |
applied to |
applied to |
4915 |
|
|
4916 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
4917 |
|
|
4918 |
it yields "no match" quickly. However, if a possessive quantifier is |
it yields "no match" quickly. However, if a possessive quantifier is |
4919 |
not used, the match runs for a very long time indeed because there are |
not used, the match runs for a very long time indeed because there are |
4920 |
so many different ways the + and * repeats can carve up the subject, |
so many different ways the + and * repeats can carve up the subject, |
4921 |
and all have to be tested before failure can be reported. |
and all have to be tested before failure can be reported. |
4922 |
|
|
4923 |
At the end of a match, the values of capturing parentheses are those |
At the end of a match, the values of capturing parentheses are those |
4924 |
from the outermost level. If you want to obtain intermediate values, a |
from the outermost level. If you want to obtain intermediate values, a |
4925 |
callout function can be used (see below and the pcrecallout documenta- |
callout function can be used (see below and the pcrecallout documenta- |
4926 |
tion). If the pattern above is matched against |
tion). If the pattern above is matched against |
4927 |
|
|
4928 |
(ab(cd)ef) |
(ab(cd)ef) |
4929 |
|
|
4930 |
the value for the inner capturing parentheses (numbered 2) is "ef", |
the value for the inner capturing parentheses (numbered 2) is "ef", |
4931 |
which is the last value taken on at the top level. If a capturing sub- |
which is the last value taken on at the top level. If a capturing sub- |
4932 |
pattern is not matched at the top level, its final value is unset, even |
pattern is not matched at the top level, its final value is unset, even |
4933 |
if it is (temporarily) set at a deeper level. |
if it is (temporarily) set at a deeper level. |
4934 |
|
|
4935 |
If there are more than 15 capturing parentheses in a pattern, PCRE has |
If there are more than 15 capturing parentheses in a pattern, PCRE has |
4936 |
to obtain extra memory to store data during a recursion, which it does |
to obtain extra memory to store data during a recursion, which it does |
4937 |
by using pcre_malloc, freeing it via pcre_free afterwards. If no memory |
by using pcre_malloc, freeing it via pcre_free afterwards. If no memory |
4938 |
can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error. |
can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error. |
4939 |
|
|
4940 |
Do not confuse the (?R) item with the condition (R), which tests for |
Do not confuse the (?R) item with the condition (R), which tests for |
4941 |
recursion. Consider this pattern, which matches text in angle brack- |
recursion. Consider this pattern, which matches text in angle brack- |
4942 |
ets, allowing for arbitrary nesting. Only digits are allowed in nested |
ets, allowing for arbitrary nesting. Only digits are allowed in nested |
4943 |
brackets (that is, when recursing), whereas any characters are permit- |
brackets (that is, when recursing), whereas any characters are permit- |
4944 |
ted at the outer level. |
ted at the outer level. |
4945 |
|
|
4946 |
< (?: (?(R) \d++ | [^<>]*+) | (?R)) * > |
< (?: (?(R) \d++ | [^<>]*+) | (?R)) * > |
4947 |
|
|
4948 |
In this pattern, (?(R) is the start of a conditional subpattern, with |
In this pattern, (?(R) is the start of a conditional subpattern, with |
4949 |
two different alternatives for the recursive and non-recursive cases. |
two different alternatives for the recursive and non-recursive cases. |
4950 |
The (?R) item is the actual recursive call. |
The (?R) item is the actual recursive call. |
4951 |
|
|
4952 |
Recursion difference from Perl |
Recursion difference from Perl |
4953 |
|
|
4954 |
In PCRE (like Python, but unlike Perl), a recursive subpattern call is |
In PCRE (like Python, but unlike Perl), a recursive subpattern call is |
4955 |
always treated as an atomic group. That is, once it has matched some of |
always treated as an atomic group. That is, once it has matched some of |
4956 |
the subject string, it is never re-entered, even if it contains untried |
the subject string, it is never re-entered, even if it contains untried |
4957 |
alternatives and there is a subsequent matching failure. This can be |
alternatives and there is a subsequent matching failure. This can be |
4958 |
illustrated by the following pattern, which purports to match a palin- |
illustrated by the following pattern, which purports to match a palin- |
4959 |
dromic string that contains an odd number of characters (for example, |
dromic string that contains an odd number of characters (for example, |
4960 |
"a", "aba", "abcba", "abcdcba"): |
"a", "aba", "abcba", "abcdcba"): |
4961 |
|
|
4962 |
^(.|(.)(?1)\2)$ |
^(.|(.)(?1)\2)$ |
4963 |
|
|
4964 |
The idea is that it either matches a single character, or two identical |
The idea is that it either matches a single character, or two identical |
4965 |
characters surrounding a sub-palindrome. In Perl, this pattern works; |
characters surrounding a sub-palindrome. In Perl, this pattern works; |
4966 |
in PCRE it does not if the pattern is longer than three characters. |
in PCRE it does not if the pattern is longer than three characters. |
4967 |
Consider the subject string "abcba": |
Consider the subject string "abcba": |
4968 |
|
|
4969 |
At the top level, the first character is matched, but as it is not at |
At the top level, the first character is matched, but as it is not at |
4970 |
the end of the string, the first alternative fails; the second alterna- |
the end of the string, the first alternative fails; the second alterna- |
4971 |
tive is taken and the recursion kicks in. The recursive call to subpat- |
tive is taken and the recursion kicks in. The recursive call to subpat- |
4972 |
tern 1 successfully matches the next character ("b"). (Note that the |
tern 1 successfully matches the next character ("b"). (Note that the |
4973 |
beginning and end of line tests are not part of the recursion). |
beginning and end of line tests are not part of the recursion). |
4974 |
|
|
4975 |
Back at the top level, the next character ("c") is compared with what |
Back at the top level, the next character ("c") is compared with what |
4976 |
subpattern 2 matched, which was "a". This fails. Because the recursion |
subpattern 2 matched, which was "a". This fails. Because the recursion |
4977 |
is treated as an atomic group, there are now no backtracking points, |
is treated as an atomic group, there are now no backtracking points, |
4978 |
and so the entire match fails. (Perl is able, at this point, to re- |
and so the entire match fails. (Perl is able, at this point, to re- |
4979 |
enter the recursion and try the second alternative.) However, if the |
enter the recursion and try the second alternative.) However, if the |
4980 |
pattern is written with the alternatives in the other order, things are |
pattern is written with the alternatives in the other order, things are |
4981 |
different: |
different: |
4982 |
|
|
4983 |
^((.)(?1)\2|.)$ |
^((.)(?1)\2|.)$ |
4984 |
|
|
4985 |
This time, the recursing alternative is tried first, and continues to |
This time, the recursing alternative is tried first, and continues to |
4986 |
recurse until it runs out of characters, at which point the recursion |
recurse until it runs out of characters, at which point the recursion |
4987 |
fails. But this time we do have another alternative to try at the |
fails. But this time we do have another alternative to try at the |
4988 |
higher level. That is the big difference: in the previous case the |
higher level. That is the big difference: in the previous case the |
4989 |
remaining alternative is at a deeper recursion level, which PCRE cannot |
remaining alternative is at a deeper recursion level, which PCRE cannot |
4990 |
use. |
use. |
4991 |
|
|
4992 |
To change the pattern so that matches all palindromic strings, not just |
To change the pattern so that matches all palindromic strings, not just |
4993 |
those with an odd number of characters, it is tempting to change the |
those with an odd number of characters, it is tempting to change the |
4994 |
pattern to this: |
pattern to this: |
4995 |
|
|
4996 |
^((.)(?1)\2|.?)$ |
^((.)(?1)\2|.?)$ |
4997 |
|
|
4998 |
Again, this works in Perl, but not in PCRE, and for the same reason. |
Again, this works in Perl, but not in PCRE, and for the same reason. |
4999 |
When a deeper recursion has matched a single character, it cannot be |
When a deeper recursion has matched a single character, it cannot be |
5000 |
entered again in order to match an empty string. The solution is to |
entered again in order to match an empty string. The solution is to |
5001 |
separate the two cases, and write out the odd and even cases as alter- |
separate the two cases, and write out the odd and even cases as alter- |
5002 |
natives at the higher level: |
natives at the higher level: |
5003 |
|
|
5004 |
^(?:((.)(?1)\2|)|((.)(?3)\4|.)) |
^(?:((.)(?1)\2|)|((.)(?3)\4|.)) |
5005 |
|
|
5006 |
If you want to match typical palindromic phrases, the pattern has to |
If you want to match typical palindromic phrases, the pattern has to |
5007 |
ignore all non-word characters, which can be done like this: |
ignore all non-word characters, which can be done like this: |
5008 |
|
|
5009 |
^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$ |
^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$ |
5010 |
|
|
5011 |
If run with the PCRE_CASELESS option, this pattern matches phrases such |
If run with the PCRE_CASELESS option, this pattern matches phrases such |
5012 |
as "A man, a plan, a canal: Panama!" and it works well in both PCRE and |
as "A man, a plan, a canal: Panama!" and it works well in both PCRE and |
5013 |
Perl. Note the use of the possessive quantifier *+ to avoid backtrack- |
Perl. Note the use of the possessive quantifier *+ to avoid backtrack- |
5014 |
ing into sequences of non-word characters. Without this, PCRE takes a |
ing into sequences of non-word characters. Without this, PCRE takes a |
5015 |
great deal longer (ten times or more) to match typical phrases, and |
great deal longer (ten times or more) to match typical phrases, and |
5016 |
Perl takes so long that you think it has gone into a loop. |
Perl takes so long that you think it has gone into a loop. |
5017 |
|
|
5018 |
WARNING: The palindrome-matching patterns above work only if the sub- |
WARNING: The palindrome-matching patterns above work only if the sub- |
5019 |
ject string does not start with a palindrome that is shorter than the |
ject string does not start with a palindrome that is shorter than the |
5020 |
entire string. For example, although "abcba" is correctly matched, if |
entire string. For example, although "abcba" is correctly matched, if |
5021 |
the subject is "ababa", PCRE finds the palindrome "aba" at the start, |
the subject is "ababa", PCRE finds the palindrome "aba" at the start, |
5022 |
then fails at top level because the end of the string does not follow. |
then fails at top level because the end of the string does not follow. |
5023 |
Once again, it cannot jump back into the recursion to try other alter- |
Once again, it cannot jump back into the recursion to try other alter- |
5024 |
natives, so the entire match fails. |
natives, so the entire match fails. |
5025 |
|
|
5026 |
|
|
5027 |
SUBPATTERNS AS SUBROUTINES |
SUBPATTERNS AS SUBROUTINES |
5028 |
|
|
5029 |
If the syntax for a recursive subpattern reference (either by number or |
If the syntax for a recursive subpattern reference (either by number or |
5030 |
by name) is used outside the parentheses to which it refers, it oper- |
by name) is used outside the parentheses to which it refers, it oper- |
5031 |
ates like a subroutine in a programming language. The "called" subpat- |
ates like a subroutine in a programming language. The "called" subpat- |
5032 |
tern may be defined before or after the reference. A numbered reference |
tern may be defined before or after the reference. A numbered reference |
5033 |
can be absolute or relative, as in these examples: |
can be absolute or relative, as in these examples: |
5034 |
|
|
5040 |
|
|
5041 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
5042 |
|
|
5043 |
matches "sense and sensibility" and "response and responsibility", but |
matches "sense and sensibility" and "response and responsibility", but |
5044 |
not "sense and responsibility". If instead the pattern |
not "sense and responsibility". If instead the pattern |
5045 |
|
|
5046 |
(sens|respons)e and (?1)ibility |
(sens|respons)e and (?1)ibility |
5047 |
|
|
5048 |
is used, it does match "sense and responsibility" as well as the other |
is used, it does match "sense and responsibility" as well as the other |
5049 |
two strings. Another example is given in the discussion of DEFINE |
two strings. Another example is given in the discussion of DEFINE |
5050 |
above. |
above. |
5051 |
|
|
5052 |
Like recursive subpatterns, a subroutine call is always treated as an |
Like recursive subpatterns, a subroutine call is always treated as an |
5053 |
atomic group. That is, once it has matched some of the subject string, |
atomic group. That is, once it has matched some of the subject string, |
5054 |
it is never re-entered, even if it contains untried alternatives and |
it is never re-entered, even if it contains untried alternatives and |
5055 |
there is a subsequent matching failure. Any capturing parentheses that |
there is a subsequent matching failure. Any capturing parentheses that |
5056 |
are set during the subroutine call revert to their previous values |
are set during the subroutine call revert to their previous values |
5057 |
afterwards. |
afterwards. |
5058 |
|
|
5059 |
When a subpattern is used as a subroutine, processing options such as |
When a subpattern is used as a subroutine, processing options such as |
5060 |
case-independence are fixed when the subpattern is defined. They cannot |
case-independence are fixed when the subpattern is defined. They cannot |
5061 |
be changed for different calls. For example, consider this pattern: |
be changed for different calls. For example, consider this pattern: |
5062 |
|
|
5063 |
(abc)(?i:(?-1)) |
(abc)(?i:(?-1)) |
5064 |
|
|
5065 |
It matches "abcabc". It does not match "abcABC" because the change of |
It matches "abcabc". It does not match "abcABC" because the change of |
5066 |
processing option does not affect the called subpattern. |
processing option does not affect the called subpattern. |
5067 |
|
|
5068 |
|
|
5069 |
ONIGURUMA SUBROUTINE SYNTAX |
ONIGURUMA SUBROUTINE SYNTAX |
5070 |
|
|
5071 |
For compatibility with Oniguruma, the non-Perl syntax \g followed by a |
For compatibility with Oniguruma, the non-Perl syntax \g followed by a |
5072 |
name or a number enclosed either in angle brackets or single quotes, is |
name or a number enclosed either in angle brackets or single quotes, is |
5073 |
an alternative syntax for referencing a subpattern as a subroutine, |
an alternative syntax for referencing a subpattern as a subroutine, |
5074 |
possibly recursively. Here are two of the examples used above, rewrit- |
possibly recursively. Here are two of the examples used above, rewrit- |
5075 |
ten using this syntax: |
ten using this syntax: |
5076 |
|
|
5077 |
(?<pn> \( ( (?>[^()]+) | \g<pn> )* \) ) |
(?<pn> \( ( (?>[^()]+) | \g<pn> )* \) ) |
5078 |
(sens|respons)e and \g'1'ibility |
(sens|respons)e and \g'1'ibility |
5079 |
|
|
5080 |
PCRE supports an extension to Oniguruma: if a number is preceded by a |
PCRE supports an extension to Oniguruma: if a number is preceded by a |
5081 |
plus or a minus sign it is taken as a relative reference. For example: |
plus or a minus sign it is taken as a relative reference. For example: |
5082 |
|
|
5083 |
(abc)(?i:\g<-1>) |
(abc)(?i:\g<-1>) |
5084 |
|
|
5085 |
Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not |
Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not |
5086 |
synonymous. The former is a back reference; the latter is a subroutine |
synonymous. The former is a back reference; the latter is a subroutine |
5087 |
call. |
call. |
5088 |
|
|
5089 |
|
|
5090 |
CALLOUTS |
CALLOUTS |
5091 |
|
|
5092 |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary |
5093 |
Perl code to be obeyed in the middle of matching a regular expression. |
Perl code to be obeyed in the middle of matching a regular expression. |
5094 |
This makes it possible, amongst other things, to extract different sub- |
This makes it possible, amongst other things, to extract different sub- |
5095 |
strings that match the same pair of parentheses when there is a repeti- |
strings that match the same pair of parentheses when there is a repeti- |
5096 |
tion. |
tion. |
5097 |
|
|
5098 |
PCRE provides a similar feature, but of course it cannot obey arbitrary |
PCRE provides a similar feature, but of course it cannot obey arbitrary |
5099 |
Perl code. The feature is called "callout". The caller of PCRE provides |
Perl code. The feature is called "callout". The caller of PCRE provides |
5100 |
an external function by putting its entry point in the global variable |
an external function by putting its entry point in the global variable |
5101 |
pcre_callout. By default, this variable contains NULL, which disables |
pcre_callout. By default, this variable contains NULL, which disables |
5102 |
all calling out. |
all calling out. |
5103 |
|
|
5104 |
Within a regular expression, (?C) indicates the points at which the |
Within a regular expression, (?C) indicates the points at which the |
5105 |
external function is to be called. If you want to identify different |
external function is to be called. If you want to identify different |
5106 |
callout points, you can put a number less than 256 after the letter C. |
callout points, you can put a number less than 256 after the letter C. |
5107 |
The default value is zero. For example, this pattern has two callout |
The default value is zero. For example, this pattern has two callout |
5108 |
points: |
points: |
5109 |
|
|
5110 |
(?C1)abc(?C2)def |
(?C1)abc(?C2)def |
5111 |
|
|
5112 |
If the PCRE_AUTO_CALLOUT flag is passed to pcre_compile(), callouts are |
If the PCRE_AUTO_CALLOUT flag is passed to pcre_compile(), callouts are |
5113 |
automatically installed before each item in the pattern. They are all |
automatically installed before each item in the pattern. They are all |
5114 |
numbered 255. |
numbered 255. |
5115 |
|
|
5116 |
During matching, when PCRE reaches a callout point (and pcre_callout is |
During matching, when PCRE reaches a callout point (and pcre_callout is |
5117 |
set), the external function is called. It is provided with the number |
set), the external function is called. It is provided with the number |
5118 |
of the callout, the position in the pattern, and, optionally, one item |
of the callout, the position in the pattern, and, optionally, one item |
5119 |
of data originally supplied by the caller of pcre_exec(). The callout |
of data originally supplied by the caller of pcre_exec(). The callout |
5120 |
function may cause matching to proceed, to backtrack, or to fail alto- |
function may cause matching to proceed, to backtrack, or to fail alto- |
5121 |
gether. A complete description of the interface to the callout function |
gether. A complete description of the interface to the callout function |
5122 |
is given in the pcrecallout documentation. |
is given in the pcrecallout documentation. |
5123 |
|
|
5124 |
|
|
5125 |
BACKTRACKING CONTROL |
BACKTRACKING CONTROL |
5126 |
|
|
5127 |
Perl 5.10 introduced a number of "Special Backtracking Control Verbs", |
Perl 5.10 introduced a number of "Special Backtracking Control Verbs", |
5128 |
which are described in the Perl documentation as "experimental and sub- |
which are described in the Perl documentation as "experimental and sub- |
5129 |
ject to change or removal in a future version of Perl". It goes on to |
ject to change or removal in a future version of Perl". It goes on to |
5130 |
say: "Their usage in production code should be noted to avoid problems |
say: "Their usage in production code should be noted to avoid problems |
5131 |
during upgrades." The same remarks apply to the PCRE features described |
during upgrades." The same remarks apply to the PCRE features described |
5132 |
in this section. |
in this section. |
5133 |
|
|
5134 |
Since these verbs are specifically related to backtracking, most of |
Since these verbs are specifically related to backtracking, most of |
5135 |
them can be used only when the pattern is to be matched using |
them can be used only when the pattern is to be matched using |
5136 |
pcre_exec(), which uses a backtracking algorithm. With the exception of |
pcre_exec(), which uses a backtracking algorithm. With the exception of |
5137 |
(*FAIL), which behaves like a failing negative assertion, they cause an |
(*FAIL), which behaves like a failing negative assertion, they cause an |
5138 |
error if encountered by pcre_dfa_exec(). |
error if encountered by pcre_dfa_exec(). |
5139 |
|
|
5140 |
If any of these verbs are used in an assertion or subroutine subpattern |
If any of these verbs are used in an assertion or subroutine subpattern |
5141 |
(including recursive subpatterns), their effect is confined to that |
(including recursive subpatterns), their effect is confined to that |
5142 |
subpattern; it does not extend to the surrounding pattern. Note that |
subpattern; it does not extend to the surrounding pattern. Note that |
5143 |
such subpatterns are processed as anchored at the point where they are |
such subpatterns are processed as anchored at the point where they are |
5144 |
tested. |
tested. |
5145 |
|
|
5146 |
The new verbs make use of what was previously invalid syntax: an open- |
The new verbs make use of what was previously invalid syntax: an open- |
5147 |
ing parenthesis followed by an asterisk. In Perl, they are generally of |
ing parenthesis followed by an asterisk. In Perl, they are generally of |
5148 |
the form (*VERB:ARG) but PCRE does not support the use of arguments, so |
the form (*VERB:ARG) but PCRE does not support the use of arguments, so |
5149 |
its general form is just (*VERB). Any number of these verbs may occur |
its general form is just (*VERB). Any number of these verbs may occur |
5150 |
in a pattern. There are two kinds: |
in a pattern. There are two kinds: |
5151 |
|
|
5152 |
Verbs that act immediately |
Verbs that act immediately |
5155 |
|
|
5156 |
(*ACCEPT) |
(*ACCEPT) |
5157 |
|
|
5158 |
This verb causes the match to end successfully, skipping the remainder |
This verb causes the match to end successfully, skipping the remainder |
5159 |
of the pattern. When inside a recursion, only the innermost pattern is |
of the pattern. When inside a recursion, only the innermost pattern is |
5160 |
ended immediately. If (*ACCEPT) is inside capturing parentheses, the |
ended immediately. If (*ACCEPT) is inside capturing parentheses, the |
5161 |
data so far is captured. (This feature was added to PCRE at release |
data so far is captured. (This feature was added to PCRE at release |
5162 |
8.00.) For example: |
8.00.) For example: |
5163 |
|
|
5164 |
A((?:A|B(*ACCEPT)|C)D) |
A((?:A|B(*ACCEPT)|C)D) |
5165 |
|
|
5166 |
This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap- |
This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap- |
5167 |
tured by the outer parentheses. |
tured by the outer parentheses. |
5168 |
|
|
5169 |
(*FAIL) or (*F) |
(*FAIL) or (*F) |
5170 |
|
|
5171 |
This verb causes the match to fail, forcing backtracking to occur. It |
This verb causes the match to fail, forcing backtracking to occur. It |
5172 |
is equivalent to (?!) but easier to read. The Perl documentation notes |
is equivalent to (?!) but easier to read. The Perl documentation notes |
5173 |
that it is probably useful only when combined with (?{}) or (??{}). |
that it is probably useful only when combined with (?{}) or (??{}). |
5174 |
Those are, of course, Perl features that are not present in PCRE. The |
Those are, of course, Perl features that are not present in PCRE. The |
5175 |
nearest equivalent is the callout feature, as for example in this pat- |
nearest equivalent is the callout feature, as for example in this pat- |
5176 |
tern: |
tern: |
5177 |
|
|
5178 |
a+(?C)(*FAIL) |
a+(?C)(*FAIL) |
5179 |
|
|
5180 |
A match with the string "aaaa" always fails, but the callout is taken |
A match with the string "aaaa" always fails, but the callout is taken |
5181 |
before each backtrack happens (in this example, 10 times). |
before each backtrack happens (in this example, 10 times). |
5182 |
|
|
5183 |
Verbs that act after backtracking |
Verbs that act after backtracking |
5184 |
|
|
5185 |
The following verbs do nothing when they are encountered. Matching con- |
The following verbs do nothing when they are encountered. Matching con- |
5186 |
tinues with what follows, but if there is no subsequent match, a fail- |
tinues with what follows, but if there is no subsequent match, a fail- |
5187 |
ure is forced. The verbs differ in exactly what kind of failure |
ure is forced. The verbs differ in exactly what kind of failure |
5188 |
occurs. |
occurs. |
5189 |
|
|
5190 |
(*COMMIT) |
(*COMMIT) |
5191 |
|
|
5192 |
This verb causes the whole match to fail outright if the rest of the |
This verb causes the whole match to fail outright if the rest of the |
5193 |
pattern does not match. Even if the pattern is unanchored, no further |
pattern does not match. Even if the pattern is unanchored, no further |
5194 |
attempts to find a match by advancing the starting point take place. |
attempts to find a match by advancing the starting point take place. |
5195 |
Once (*COMMIT) has been passed, pcre_exec() is committed to finding a |
Once (*COMMIT) has been passed, pcre_exec() is committed to finding a |
5196 |
match at the current starting point, or not at all. For example: |
match at the current starting point, or not at all. For example: |
5197 |
|
|
5198 |
a+(*COMMIT)b |
a+(*COMMIT)b |
5199 |
|
|
5200 |
This matches "xxaab" but not "aacaab". It can be thought of as a kind |
This matches "xxaab" but not "aacaab". It can be thought of as a kind |
5201 |
of dynamic anchor, or "I've started, so I must finish." |
of dynamic anchor, or "I've started, so I must finish." |
5202 |
|
|
5203 |
(*PRUNE) |
(*PRUNE) |
5204 |
|
|
5205 |
This verb causes the match to fail at the current position if the rest |
This verb causes the match to fail at the current position if the rest |
5206 |
of the pattern does not match. If the pattern is unanchored, the normal |
of the pattern does not match. If the pattern is unanchored, the normal |
5207 |
"bumpalong" advance to the next starting character then happens. Back- |
"bumpalong" advance to the next starting character then happens. Back- |
5208 |
tracking can occur as usual to the left of (*PRUNE), or when matching |
tracking can occur as usual to the left of (*PRUNE), or when matching |
5209 |
to the right of (*PRUNE), but if there is no match to the right, back- |
to the right of (*PRUNE), but if there is no match to the right, back- |
5210 |
tracking cannot cross (*PRUNE). In simple cases, the use of (*PRUNE) |
tracking cannot cross (*PRUNE). In simple cases, the use of (*PRUNE) |
5211 |
is just an alternative to an atomic group or possessive quantifier, but |
is just an alternative to an atomic group or possessive quantifier, but |
5212 |
there are some uses of (*PRUNE) that cannot be expressed in any other |
there are some uses of (*PRUNE) that cannot be expressed in any other |
5213 |
way. |
way. |
5214 |
|
|
5215 |
(*SKIP) |
(*SKIP) |
5216 |
|
|
5217 |
This verb is like (*PRUNE), except that if the pattern is unanchored, |
This verb is like (*PRUNE), except that if the pattern is unanchored, |
5218 |
the "bumpalong" advance is not to the next character, but to the posi- |
the "bumpalong" advance is not to the next character, but to the posi- |
5219 |
tion in the subject where (*SKIP) was encountered. (*SKIP) signifies |
tion in the subject where (*SKIP) was encountered. (*SKIP) signifies |
5220 |
that whatever text was matched leading up to it cannot be part of a |
that whatever text was matched leading up to it cannot be part of a |
5221 |
successful match. Consider: |
successful match. Consider: |
5222 |
|
|
5223 |
a+(*SKIP)b |
a+(*SKIP)b |
5224 |
|
|
5225 |
If the subject is "aaaac...", after the first match attempt fails |
If the subject is "aaaac...", after the first match attempt fails |
5226 |
(starting at the first character in the string), the starting point |
(starting at the first character in the string), the starting point |
5227 |
skips on to start the next attempt at "c". Note that a possessive quan- |
skips on to start the next attempt at "c". Note that a possessive quan- |
5228 |
tifer does not have the same effect as this example; although it would |
tifer does not have the same effect as this example; although it would |
5229 |
suppress backtracking during the first match attempt, the second |
suppress backtracking during the first match attempt, the second |
5230 |
attempt would start at the second character instead of skipping on to |
attempt would start at the second character instead of skipping on to |
5231 |
"c". |
"c". |
5232 |
|
|
5233 |
(*THEN) |
(*THEN) |
5234 |
|
|
5235 |
This verb causes a skip to the next alternation if the rest of the pat- |
This verb causes a skip to the next alternation if the rest of the pat- |
5236 |
tern does not match. That is, it cancels pending backtracking, but only |
tern does not match. That is, it cancels pending backtracking, but only |
5237 |
within the current alternation. Its name comes from the observation |
within the current alternation. Its name comes from the observation |
5238 |
that it can be used for a pattern-based if-then-else block: |
that it can be used for a pattern-based if-then-else block: |
5239 |
|
|
5240 |
( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... |
( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... |
5241 |
|
|
5242 |
If the COND1 pattern matches, FOO is tried (and possibly further items |
If the COND1 pattern matches, FOO is tried (and possibly further items |
5243 |
after the end of the group if FOO succeeds); on failure the matcher |
after the end of the group if FOO succeeds); on failure the matcher |
5244 |
skips to the second alternative and tries COND2, without backtracking |
skips to the second alternative and tries COND2, without backtracking |
5245 |
into COND1. If (*THEN) is used outside of any alternation, it acts |
into COND1. If (*THEN) is used outside of any alternation, it acts |
5246 |
exactly like (*PRUNE). |
exactly like (*PRUNE). |
5247 |
|
|
5248 |
|
|
5260 |
|
|
5261 |
REVISION |
REVISION |
5262 |
|
|
5263 |
Last updated: 11 January 2010 |
Last updated: 06 March 2010 |
5264 |
Copyright (c) 1997-2010 University of Cambridge. |
Copyright (c) 1997-2010 University of Cambridge. |
5265 |
------------------------------------------------------------------------------ |
------------------------------------------------------------------------------ |
5266 |
|
|
5372 |
|
|
5373 |
SCRIPT NAMES FOR \p AND \P |
SCRIPT NAMES FOR \p AND \P |
5374 |
|
|
5375 |
Arabic, Armenian, Balinese, Bengali, Bopomofo, Braille, Buginese, |
Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille, |
5376 |
Buhid, Canadian_Aboriginal, Carian, Cham, Cherokee, Common, Coptic, Cu- |
Buginese, Buhid, Canadian_Aboriginal, Carian, Cham, Cherokee, Common, |
5377 |
neiform, Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, |
Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, Devanagari, Egyp- |
5378 |
Glagolitic, Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, |
tian_Hieroglyphs, Ethiopic, Georgian, Glagolitic, Gothic, Greek, |
5379 |
Hebrew, Hiragana, Inherited, Kannada, Katakana, Kayah_Li, Kharoshthi, |
Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Impe- |
5380 |
Khmer, Lao, Latin, Lepcha, Limbu, Linear_B, Lycian, Lydian, Malayalam, |
rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian, |
5381 |
Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic, Old_Persian, |
Javanese, Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao, |
5382 |
Ol_Chiki, Oriya, Osmanya, Phags_Pa, Phoenician, Rejang, Runic, Saurash- |
Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian, Lydian, Malayalam, |
5383 |
tra, Shavian, Sinhala, Sudanese, Syloti_Nagri, Syriac, Tagalog, Tag- |
Meetei_Mayek, Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic, |
5384 |
banwa, Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, |
Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki, Oriya, Osmanya, |
5385 |
|
Phags_Pa, Phoenician, Rejang, Runic, Samaritan, Saurashtra, Shavian, |
5386 |
|
Sinhala, Sundanese, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, |
5387 |
|
Tai_Tham, Tai_Viet, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, |
5388 |
Ugaritic, Vai, Yi. |
Ugaritic, Vai, Yi. |
5389 |
|
|
5390 |
|
|
5564 |
(*ACCEPT) force successful match |
(*ACCEPT) force successful match |
5565 |
(*FAIL) force backtrack; synonym (*F) |
(*FAIL) force backtrack; synonym (*F) |
5566 |
|
|
5567 |
The following act only when a subsequent match failure causes a back- |
The following act only when a subsequent match failure causes a back- |
5568 |
track to reach them. They all force a match failure, but they differ in |
track to reach them. They all force a match failure, but they differ in |
5569 |
what happens afterwards. Those that advance the start-of-match point do |
what happens afterwards. Those that advance the start-of-match point do |
5570 |
so only if the pattern is not anchored. |
so only if the pattern is not anchored. |
5577 |
|
|
5578 |
NEWLINE CONVENTIONS |
NEWLINE CONVENTIONS |
5579 |
|
|
5580 |
These are recognized only at the very start of the pattern or after a |
These are recognized only at the very start of the pattern or after a |
5581 |
(*BSR_...) or (*UTF8) option. |
(*BSR_...) or (*UTF8) option. |
5582 |
|
|
5583 |
(*CR) carriage return only |
(*CR) carriage return only |
5589 |
|
|
5590 |
WHAT \R MATCHES |
WHAT \R MATCHES |
5591 |
|
|
5592 |
These are recognized only at the very start of the pattern or after a |
These are recognized only at the very start of the pattern or after a |
5593 |
(*...) option that sets the newline convention or UTF-8 mode. |
(*...) option that sets the newline convention or UTF-8 mode. |
5594 |
|
|
5595 |
(*BSR_ANYCRLF) CR, LF, or CRLF |
(*BSR_ANYCRLF) CR, LF, or CRLF |
5616 |
|
|
5617 |
REVISION |
REVISION |
5618 |
|
|
5619 |
Last updated: 11 April 2009 |
Last updated: 01 March 2010 |
5620 |
Copyright (c) 1997-2009 University of Cambridge. |
Copyright (c) 1997-2010 University of Cambridge. |
5621 |
------------------------------------------------------------------------------ |
------------------------------------------------------------------------------ |
5622 |
|
|
5623 |
|
|
6141 |
can affect both of them. |
can affect both of them. |
6142 |
|
|
6143 |
|
|
6144 |
MEMORY USAGE |
COMPILED PATTERN MEMORY USAGE |
6145 |
|
|
6146 |
Patterns are compiled by PCRE into a reasonably efficient byte code, so |
Patterns are compiled by PCRE into a reasonably efficient byte code, so |
6147 |
that most simple patterns do not use much memory. However, there is one |
that most simple patterns do not use much memory. However, there is one |
6148 |
case where memory usage can be unexpectedly large. When a parenthesized |
case where the memory usage of a compiled pattern can be unexpectedly |
6149 |
subpattern has a quantifier with a minimum greater than 1 and/or a lim- |
large. If a parenthesized subpattern has a quantifier with a minimum |
6150 |
ited maximum, the whole subpattern is repeated in the compiled code. |
greater than 1 and/or a limited maximum, the whole subpattern is |
6151 |
For example, the pattern |
repeated in the compiled code. For example, the pattern |
6152 |
|
|
6153 |
(abc|def){2,4} |
(abc|def){2,4} |
6154 |
|
|
6190 |
otherwise handle. |
otherwise handle. |
6191 |
|
|
6192 |
|
|
6193 |
|
STACK USAGE AT RUN TIME |
6194 |
|
|
6195 |
|
When pcre_exec() is used for matching, certain kinds of pattern can |
6196 |
|
cause it to use large amounts of the process stack. In some environ- |
6197 |
|
ments the default process stack is quite small, and if it runs out the |
6198 |
|
result is often SIGSEGV. This issue is probably the most frequently |
6199 |
|
raised problem with PCRE. Rewriting your pattern can often help. The |
6200 |
|
pcrestack documentation discusses this issue in detail. |
6201 |
|
|
6202 |
|
|
6203 |
PROCESSING TIME |
PROCESSING TIME |
6204 |
|
|
6205 |
Certain items in regular expression patterns are processed more effi- |
Certain items in regular expression patterns are processed more effi- |
6206 |
ciently than others. It is more efficient to use a character class like |
ciently than others. It is more efficient to use a character class like |
6207 |
[aeiou] than a set of single-character alternatives such as |
[aeiou] than a set of single-character alternatives such as |
6208 |
(a|e|i|o|u). In general, the simplest construction that provides the |
(a|e|i|o|u). In general, the simplest construction that provides the |
6209 |
required behaviour is usually the most efficient. Jeffrey Friedl's book |
required behaviour is usually the most efficient. Jeffrey Friedl's book |
6210 |
contains a lot of useful general discussion about optimizing regular |
contains a lot of useful general discussion about optimizing regular |
6211 |
expressions for efficient performance. This document contains a few |
expressions for efficient performance. This document contains a few |
6212 |
observations about PCRE. |
observations about PCRE. |
6213 |
|
|
6214 |
Using Unicode character properties (the \p, \P, and \X escapes) is |
Using Unicode character properties (the \p, \P, and \X escapes) is |
6215 |
slow, because PCRE has to scan a structure that contains data for over |
slow, because PCRE has to scan a structure that contains data for over |
6216 |
fifteen thousand characters whenever it needs a character's property. |
fifteen thousand characters whenever it needs a character's property. |
6217 |
If you can find an alternative pattern that does not use character |
If you can find an alternative pattern that does not use character |
6218 |
properties, it will probably be faster. |
properties, it will probably be faster. |
6219 |
|
|
6220 |
When a pattern begins with .* not in parentheses, or in parentheses |
When a pattern begins with .* not in parentheses, or in parentheses |
6221 |
that are not the subject of a backreference, and the PCRE_DOTALL option |
that are not the subject of a backreference, and the PCRE_DOTALL option |
6222 |
is set, the pattern is implicitly anchored by PCRE, since it can match |
is set, the pattern is implicitly anchored by PCRE, since it can match |
6223 |
only at the start of a subject string. However, if PCRE_DOTALL is not |
only at the start of a subject string. However, if PCRE_DOTALL is not |
6224 |
set, PCRE cannot make this optimization, because the . metacharacter |
set, PCRE cannot make this optimization, because the . metacharacter |
6225 |
does not then match a newline, and if the subject string contains new- |
does not then match a newline, and if the subject string contains new- |
6226 |
lines, the pattern may match from the character immediately following |
lines, the pattern may match from the character immediately following |
6227 |
one of them instead of from the very start. For example, the pattern |
one of them instead of from the very start. For example, the pattern |
6228 |
|
|
6229 |
.*second |
.*second |
6230 |
|
|
6231 |
matches the subject "first\nand second" (where \n stands for a newline |
matches the subject "first\nand second" (where \n stands for a newline |
6232 |
character), with the match starting at the seventh character. In order |
character), with the match starting at the seventh character. In order |
6233 |
to do this, PCRE has to retry the match starting after every newline in |
to do this, PCRE has to retry the match starting after every newline in |
6234 |
the subject. |
the subject. |
6235 |
|
|
6236 |
If you are using such a pattern with subject strings that do not con- |
If you are using such a pattern with subject strings that do not con- |
6237 |
tain newlines, the best performance is obtained by setting PCRE_DOTALL, |
tain newlines, the best performance is obtained by setting PCRE_DOTALL, |
6238 |
or starting the pattern with ^.* or ^.*? to indicate explicit anchor- |
or starting the pattern with ^.* or ^.*? to indicate explicit anchor- |
6239 |
ing. That saves PCRE from having to scan along the subject looking for |
ing. That saves PCRE from having to scan along the subject looking for |
6240 |
a newline to restart at. |
a newline to restart at. |
6241 |
|
|
6242 |
Beware of patterns that contain nested indefinite repeats. These can |
Beware of patterns that contain nested indefinite repeats. These can |
6243 |
take a long time to run when applied to a string that does not match. |
take a long time to run when applied to a string that does not match. |
6244 |
Consider the pattern fragment |
Consider the pattern fragment |
6245 |
|
|
6246 |
^(a+)* |
^(a+)* |
6247 |
|
|
6248 |
This can match "aaaa" in 16 different ways, and this number increases |
This can match "aaaa" in 16 different ways, and this number increases |
6249 |
very rapidly as the string gets longer. (The * repeat can match 0, 1, |
very rapidly as the string gets longer. (The * repeat can match 0, 1, |
6250 |
2, 3, or 4 times, and for each of those cases other than 0 or 4, the + |
2, 3, or 4 times, and for each of those cases other than 0 or 4, the + |
6251 |
repeats can match different numbers of times.) When the remainder of |
repeats can match different numbers of times.) When the remainder of |
6252 |
the pattern is such that the entire match is going to fail, PCRE has in |
the pattern is such that the entire match is going to fail, PCRE has in |
6253 |
principle to try every possible variation, and this can take an |
principle to try every possible variation, and this can take an |
6254 |
extremely long time, even for relatively short strings. |
extremely long time, even for relatively short strings. |
6255 |
|
|
6256 |
An optimization catches some of the more simple cases such as |
An optimization catches some of the more simple cases such as |
6257 |
|
|
6258 |
(a+)*b |
(a+)*b |
6259 |
|
|
6260 |
where a literal character follows. Before embarking on the standard |
where a literal character follows. Before embarking on the standard |
6261 |
matching procedure, PCRE checks that there is a "b" later in the sub- |
matching procedure, PCRE checks that there is a "b" later in the sub- |
6262 |
ject string, and if there is not, it fails the match immediately. How- |
ject string, and if there is not, it fails the match immediately. How- |
6263 |
ever, when there is no following literal this optimization cannot be |
ever, when there is no following literal this optimization cannot be |
6264 |
used. You can see the difference by comparing the behaviour of |
used. You can see the difference by comparing the behaviour of |
6265 |
|
|
6266 |
(a+)*\d |
(a+)*\d |
6267 |
|
|
6268 |
with the pattern above. The former gives a failure almost instantly |
with the pattern above. The former gives a failure almost instantly |
6269 |
when applied to a whole line of "a" characters, whereas the latter |
when applied to a whole line of "a" characters, whereas the latter |
6270 |
takes an appreciable time with strings longer than about 20 characters. |
takes an appreciable time with strings longer than about 20 characters. |
6271 |
|
|
6272 |
In many cases, the solution to this kind of performance issue is to use |
In many cases, the solution to this kind of performance issue is to use |
6282 |
|
|
6283 |
REVISION |
REVISION |
6284 |
|
|
6285 |
Last updated: 06 March 2007 |
Last updated: 07 March 2010 |
6286 |
Copyright (c) 1997-2007 University of Cambridge. |
Copyright (c) 1997-2010 University of Cambridge. |
6287 |
------------------------------------------------------------------------------ |
------------------------------------------------------------------------------ |
6288 |
|
|
6289 |
|
|