| 908 |
dard. When PCRE is run, the default can be overridden, either when a |
dard. When PCRE is run, the default can be overridden, either when a |
| 909 |
pattern is compiled, or when it is matched. |
pattern is compiled, or when it is matched. |
| 910 |
|
|
| 911 |
|
At compile time, the newline convention can be specified by the options |
| 912 |
|
argument of pcre_compile(), or it can be specified by special text at |
| 913 |
|
the start of the pattern itself; this overrides any other settings. See |
| 914 |
|
the pcrepattern page for details of the special character sequences. |
| 915 |
|
|
| 916 |
In the PCRE documentation the word "newline" is used to mean "the char- |
In the PCRE documentation the word "newline" is used to mean "the char- |
| 917 |
acter or pair of characters that indicate a line break". The choice of |
acter or pair of characters that indicate a line break". The choice of |
| 918 |
newline convention affects the handling of the dot, circumflex, and |
newline convention affects the handling of the dot, circumflex, and |
| 919 |
dollar metacharacters, the handling of #-comments in /x mode, and, when |
dollar metacharacters, the handling of #-comments in /x mode, and, when |
| 920 |
CRLF is a recognized line ending sequence, the match position advance- |
CRLF is a recognized line ending sequence, the match position advance- |
| 921 |
ment for a non-anchored pattern. The choice of newline convention does |
ment for a non-anchored pattern. There is more detail about this in the |
| 922 |
not affect the interpretation of the \n or \r escape sequences. |
section on pcre_exec() options below. The choice of newline convention |
| 923 |
|
does not affect the interpretation of the \n or \r escape sequences. |
| 924 |
|
|
| 925 |
|
|
| 926 |
MULTITHREADING |
MULTITHREADING |
| 1528 |
returned. The fourth argument should point to an unsigned char * vari- |
returned. The fourth argument should point to an unsigned char * vari- |
| 1529 |
able. |
able. |
| 1530 |
|
|
| 1531 |
|
PCRE_INFO_HASCRORLF |
| 1532 |
|
|
| 1533 |
|
Return 1 if the pattern contains any explicit matches for CR or LF |
| 1534 |
|
characters, otherwise 0. The fourth argument should point to an int |
| 1535 |
|
variable. |
| 1536 |
|
|
| 1537 |
PCRE_INFO_JCHANGED |
PCRE_INFO_JCHANGED |
| 1538 |
|
|
| 1539 |
Return 1 if the (?J) option setting is used in the pattern, otherwise |
Return 1 if the (?J) option setting is used in the pattern, otherwise |
| 1824 |
tion of pcre_compile() above. During matching, the newline choice |
tion of pcre_compile() above. During matching, the newline choice |
| 1825 |
affects the behaviour of the dot, circumflex, and dollar metacharac- |
affects the behaviour of the dot, circumflex, and dollar metacharac- |
| 1826 |
ters. It may also alter the way the match position is advanced after a |
ters. It may also alter the way the match position is advanced after a |
| 1827 |
match failure for an unanchored pattern. When PCRE_NEWLINE_CRLF, |
match failure for an unanchored pattern. |
| 1828 |
PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is set, and a match attempt |
|
| 1829 |
fails when the current position is at a CRLF sequence, the match posi- |
When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is |
| 1830 |
tion is advanced by two characters instead of one, in other words, to |
set, and a match attempt for an unanchored pattern fails when the cur- |
| 1831 |
after the CRLF. |
rent position is at a CRLF sequence, and the pattern contains no |
| 1832 |
|
explicit matches for CR or NL characters, the match position is |
| 1833 |
|
advanced by two characters instead of one, in other words, to after the |
| 1834 |
|
CRLF. |
| 1835 |
|
|
| 1836 |
|
The above rule is a compromise that makes the most common cases work as |
| 1837 |
|
expected. For example, if the pattern is .+A (and the PCRE_DOTALL |
| 1838 |
|
option is not set), it does not match the string "\r\nA" because, after |
| 1839 |
|
failing at the start, it skips both the CR and the LF before retrying. |
| 1840 |
|
However, the pattern [\r\n]A does match that string, because it con- |
| 1841 |
|
tains an explicit CR or LF reference, and so advances only by one char- |
| 1842 |
|
acter after the first failure. Note than an explicit CR or LF refer- |
| 1843 |
|
ence occurs for negated character classes such as [^X] because they can |
| 1844 |
|
match CR or LF characters. |
| 1845 |
|
|
| 1846 |
|
Notwithstanding the above, anomalous effects may still occur when CRLF |
| 1847 |
|
is a valid newline sequence and explicit \r or \n escapes appear in the |
| 1848 |
|
pattern. |
| 1849 |
|
|
| 1850 |
PCRE_NOTBOL |
PCRE_NOTBOL |
| 1851 |
|
|
| 1852 |
This option specifies that first character of the subject string is not |
This option specifies that first character of the subject string is not |
| 1853 |
the beginning of a line, so the circumflex metacharacter should not |
the beginning of a line, so the circumflex metacharacter should not |
| 1854 |
match before it. Setting this without PCRE_MULTILINE (at compile time) |
match before it. Setting this without PCRE_MULTILINE (at compile time) |
| 1855 |
causes circumflex never to match. This option affects only the behav- |
causes circumflex never to match. This option affects only the behav- |
| 1856 |
iour of the circumflex metacharacter. It does not affect \A. |
iour of the circumflex metacharacter. It does not affect \A. |
| 1857 |
|
|
| 1858 |
PCRE_NOTEOL |
PCRE_NOTEOL |
| 1859 |
|
|
| 1860 |
This option specifies that the end of the subject string is not the end |
This option specifies that the end of the subject string is not the end |
| 1861 |
of a line, so the dollar metacharacter should not match it nor (except |
of a line, so the dollar metacharacter should not match it nor (except |
| 1862 |
in multiline mode) a newline immediately before it. Setting this with- |
in multiline mode) a newline immediately before it. Setting this with- |
| 1863 |
out PCRE_MULTILINE (at compile time) causes dollar never to match. This |
out PCRE_MULTILINE (at compile time) causes dollar never to match. This |
| 1864 |
option affects only the behaviour of the dollar metacharacter. It does |
option affects only the behaviour of the dollar metacharacter. It does |
| 1865 |
not affect \Z or \z. |
not affect \Z or \z. |
| 1866 |
|
|
| 1867 |
PCRE_NOTEMPTY |
PCRE_NOTEMPTY |
| 1868 |
|
|
| 1869 |
An empty string is not considered to be a valid match if this option is |
An empty string is not considered to be a valid match if this option is |
| 1870 |
set. If there are alternatives in the pattern, they are tried. If all |
set. If there are alternatives in the pattern, they are tried. If all |
| 1871 |
the alternatives match the empty string, the entire match fails. For |
the alternatives match the empty string, the entire match fails. For |
| 1872 |
example, if the pattern |
example, if the pattern |
| 1873 |
|
|
| 1874 |
a?b? |
a?b? |
| 1875 |
|
|
| 1876 |
is applied to a string not beginning with "a" or "b", it matches the |
is applied to a string not beginning with "a" or "b", it matches the |
| 1877 |
empty string at the start of the subject. With PCRE_NOTEMPTY set, this |
empty string at the start of the subject. With PCRE_NOTEMPTY set, this |
| 1878 |
match is not valid, so PCRE searches further into the string for occur- |
match is not valid, so PCRE searches further into the string for occur- |
| 1879 |
rences of "a" or "b". |
rences of "a" or "b". |
| 1880 |
|
|
| 1881 |
Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe- |
Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe- |
| 1882 |
cial case of a pattern match of the empty string within its split() |
cial case of a pattern match of the empty string within its split() |
| 1883 |
function, and when using the /g modifier. It is possible to emulate |
function, and when using the /g modifier. It is possible to emulate |
| 1884 |
Perl's behaviour after matching a null string by first trying the match |
Perl's behaviour after matching a null string by first trying the match |
| 1885 |
again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then |
again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then |
| 1886 |
if that fails by advancing the starting offset (see below) and trying |
if that fails by advancing the starting offset (see below) and trying |
| 1887 |
an ordinary match again. There is some code that demonstrates how to do |
an ordinary match again. There is some code that demonstrates how to do |
| 1888 |
this in the pcredemo.c sample program. |
this in the pcredemo.c sample program. |
| 1889 |
|
|
| 1890 |
PCRE_NO_UTF8_CHECK |
PCRE_NO_UTF8_CHECK |
| 1891 |
|
|
| 1892 |
When PCRE_UTF8 is set at compile time, the validity of the subject as a |
When PCRE_UTF8 is set at compile time, the validity of the subject as a |
| 1893 |
UTF-8 string is automatically checked when pcre_exec() is subsequently |
UTF-8 string is automatically checked when pcre_exec() is subsequently |
| 1894 |
called. The value of startoffset is also checked to ensure that it |
called. The value of startoffset is also checked to ensure that it |
| 1895 |
points to the start of a UTF-8 character. There is a discussion about |
points to the start of a UTF-8 character. There is a discussion about |
| 1896 |
the validity of UTF-8 strings in the section on UTF-8 support in the |
the validity of UTF-8 strings in the section on UTF-8 support in the |
| 1897 |
main pcre page. If an invalid UTF-8 sequence of bytes is found, |
main pcre page. If an invalid UTF-8 sequence of bytes is found, |
| 1898 |
pcre_exec() returns the error PCRE_ERROR_BADUTF8. If startoffset con- |
pcre_exec() returns the error PCRE_ERROR_BADUTF8. If startoffset con- |
| 1899 |
tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned. |
tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned. |
| 1900 |
|
|
| 1901 |
If you already know that your subject is valid, and you want to skip |
If you already know that your subject is valid, and you want to skip |
| 1902 |
these checks for performance reasons, you can set the |
these checks for performance reasons, you can set the |
| 1903 |
PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to |
PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to |
| 1904 |
do this for the second and subsequent calls to pcre_exec() if you are |
do this for the second and subsequent calls to pcre_exec() if you are |
| 1905 |
making repeated calls to find all the matches in a single subject |
making repeated calls to find all the matches in a single subject |
| 1906 |
string. However, you should be sure that the value of startoffset |
string. However, you should be sure that the value of startoffset |
| 1907 |
points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is |
points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is |
| 1908 |
set, the effect of passing an invalid UTF-8 string as a subject, or a |
set, the effect of passing an invalid UTF-8 string as a subject, or a |
| 1909 |
value of startoffset that does not point to the start of a UTF-8 char- |
value of startoffset that does not point to the start of a UTF-8 char- |
| 1910 |
acter, is undefined. Your program may crash. |
acter, is undefined. Your program may crash. |
| 1911 |
|
|
| 1912 |
PCRE_PARTIAL |
PCRE_PARTIAL |
| 1913 |
|
|
| 1914 |
This option turns on the partial matching feature. If the subject |
This option turns on the partial matching feature. If the subject |
| 1915 |
string fails to match the pattern, but at some point during the match- |
string fails to match the pattern, but at some point during the match- |
| 1916 |
ing process the end of the subject was reached (that is, the subject |
ing process the end of the subject was reached (that is, the subject |
| 1917 |
partially matches the pattern and the failure to match occurred only |
partially matches the pattern and the failure to match occurred only |
| 1918 |
because there were not enough subject characters), pcre_exec() returns |
because there were not enough subject characters), pcre_exec() returns |
| 1919 |
PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is |
PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is |
| 1920 |
used, there are restrictions on what may appear in the pattern. These |
used, there are restrictions on what may appear in the pattern. These |
| 1921 |
are discussed in the pcrepartial documentation. |
are discussed in the pcrepartial documentation. |
| 1922 |
|
|
| 1923 |
The string to be matched by pcre_exec() |
The string to be matched by pcre_exec() |
| 1924 |
|
|
| 1925 |
The subject string is passed to pcre_exec() as a pointer in subject, a |
The subject string is passed to pcre_exec() as a pointer in subject, a |
| 1926 |
length in length, and a starting byte offset in startoffset. In UTF-8 |
length in length, and a starting byte offset in startoffset. In UTF-8 |
| 1927 |
mode, the byte offset must point to the start of a UTF-8 character. |
mode, the byte offset must point to the start of a UTF-8 character. |
| 1928 |
Unlike the pattern string, the subject may contain binary zero bytes. |
Unlike the pattern string, the subject may contain binary zero bytes. |
| 1929 |
When the starting offset is zero, the search for a match starts at the |
When the starting offset is zero, the search for a match starts at the |
| 1930 |
beginning of the subject, and this is by far the most common case. |
beginning of the subject, and this is by far the most common case. |
| 1931 |
|
|
| 1932 |
A non-zero starting offset is useful when searching for another match |
A non-zero starting offset is useful when searching for another match |
| 1933 |
in the same subject by calling pcre_exec() again after a previous suc- |
in the same subject by calling pcre_exec() again after a previous suc- |
| 1934 |
cess. Setting startoffset differs from just passing over a shortened |
cess. Setting startoffset differs from just passing over a shortened |
| 1935 |
string and setting PCRE_NOTBOL in the case of a pattern that begins |
string and setting PCRE_NOTBOL in the case of a pattern that begins |
| 1936 |
with any kind of lookbehind. For example, consider the pattern |
with any kind of lookbehind. For example, consider the pattern |
| 1937 |
|
|
| 1938 |
\Biss\B |
\Biss\B |
| 1939 |
|
|
| 1940 |
which finds occurrences of "iss" in the middle of words. (\B matches |
which finds occurrences of "iss" in the middle of words. (\B matches |
| 1941 |
only if the current position in the subject is not a word boundary.) |
only if the current position in the subject is not a word boundary.) |
| 1942 |
When applied to the string "Mississipi" the first call to pcre_exec() |
When applied to the string "Mississipi" the first call to pcre_exec() |
| 1943 |
finds the first occurrence. If pcre_exec() is called again with just |
finds the first occurrence. If pcre_exec() is called again with just |
| 1944 |
the remainder of the subject, namely "issipi", it does not match, |
the remainder of the subject, namely "issipi", it does not match, |
| 1945 |
because \B is always false at the start of the subject, which is deemed |
because \B is always false at the start of the subject, which is deemed |
| 1946 |
to be a word boundary. However, if pcre_exec() is passed the entire |
to be a word boundary. However, if pcre_exec() is passed the entire |
| 1947 |
string again, but with startoffset set to 4, it finds the second occur- |
string again, but with startoffset set to 4, it finds the second occur- |
| 1948 |
rence of "iss" because it is able to look behind the starting point to |
rence of "iss" because it is able to look behind the starting point to |
| 1949 |
discover that it is preceded by a letter. |
discover that it is preceded by a letter. |
| 1950 |
|
|
| 1951 |
If a non-zero starting offset is passed when the pattern is anchored, |
If a non-zero starting offset is passed when the pattern is anchored, |
| 1952 |
one attempt to match at the given offset is made. This can only succeed |
one attempt to match at the given offset is made. This can only succeed |
| 1953 |
if the pattern does not require the match to be at the start of the |
if the pattern does not require the match to be at the start of the |
| 1954 |
subject. |
subject. |
| 1955 |
|
|
| 1956 |
How pcre_exec() returns captured substrings |
How pcre_exec() returns captured substrings |
| 1957 |
|
|
| 1958 |
In general, a pattern matches a certain portion of the subject, and in |
In general, a pattern matches a certain portion of the subject, and in |
| 1959 |
addition, further substrings from the subject may be picked out by |
addition, further substrings from the subject may be picked out by |
| 1960 |
parts of the pattern. Following the usage in Jeffrey Friedl's book, |
parts of the pattern. Following the usage in Jeffrey Friedl's book, |
| 1961 |
this is called "capturing" in what follows, and the phrase "capturing |
this is called "capturing" in what follows, and the phrase "capturing |
| 1962 |
subpattern" is used for a fragment of a pattern that picks out a sub- |
subpattern" is used for a fragment of a pattern that picks out a sub- |
| 1963 |
string. PCRE supports several other kinds of parenthesized subpattern |
string. PCRE supports several other kinds of parenthesized subpattern |
| 1964 |
that do not cause substrings to be captured. |
that do not cause substrings to be captured. |
| 1965 |
|
|
| 1966 |
Captured substrings are returned to the caller via a vector of integer |
Captured substrings are returned to the caller via a vector of integer |
| 1967 |
offsets whose address is passed in ovector. The number of elements in |
offsets whose address is passed in ovector. The number of elements in |
| 1968 |
the vector is passed in ovecsize, which must be a non-negative number. |
the vector is passed in ovecsize, which must be a non-negative number. |
| 1969 |
Note: this argument is NOT the size of ovector in bytes. |
Note: this argument is NOT the size of ovector in bytes. |
| 1970 |
|
|
| 1971 |
The first two-thirds of the vector is used to pass back captured sub- |
The first two-thirds of the vector is used to pass back captured sub- |
| 1972 |
strings, each substring using a pair of integers. The remaining third |
strings, each substring using a pair of integers. The remaining third |
| 1973 |
of the vector is used as workspace by pcre_exec() while matching cap- |
of the vector is used as workspace by pcre_exec() while matching cap- |
| 1974 |
turing subpatterns, and is not available for passing back information. |
turing subpatterns, and is not available for passing back information. |
| 1975 |
The length passed in ovecsize should always be a multiple of three. If |
The length passed in ovecsize should always be a multiple of three. If |
| 1976 |
it is not, it is rounded down. |
it is not, it is rounded down. |
| 1977 |
|
|
| 1978 |
When a match is successful, information about captured substrings is |
When a match is successful, information about captured substrings is |
| 1979 |
returned in pairs of integers, starting at the beginning of ovector, |
returned in pairs of integers, starting at the beginning of ovector, |
| 1980 |
and continuing up to two-thirds of its length at the most. The first |
and continuing up to two-thirds of its length at the most. The first |
| 1981 |
element of a pair is set to the offset of the first character in a sub- |
element of a pair is set to the offset of the first character in a sub- |
| 1982 |
string, and the second is set to the offset of the first character |
string, and the second is set to the offset of the first character |
| 1983 |
after the end of a substring. The first pair, ovector[0] and ovec- |
after the end of a substring. The first pair, ovector[0] and ovec- |
| 1984 |
tor[1], identify the portion of the subject string matched by the |
tor[1], identify the portion of the subject string matched by the |
| 1985 |
entire pattern. The next pair is used for the first capturing subpat- |
entire pattern. The next pair is used for the first capturing subpat- |
| 1986 |
tern, and so on. The value returned by pcre_exec() is one more than the |
tern, and so on. The value returned by pcre_exec() is one more than the |
| 1987 |
highest numbered pair that has been set. For example, if two substrings |
highest numbered pair that has been set. For example, if two substrings |
| 1988 |
have been captured, the returned value is 3. If there are no capturing |
have been captured, the returned value is 3. If there are no capturing |
| 1989 |
subpatterns, the return value from a successful match is 1, indicating |
subpatterns, the return value from a successful match is 1, indicating |
| 1990 |
that just the first pair of offsets has been set. |
that just the first pair of offsets has been set. |
| 1991 |
|
|
| 1992 |
If a capturing subpattern is matched repeatedly, it is the last portion |
If a capturing subpattern is matched repeatedly, it is the last portion |
| 1993 |
of the string that it matched that is returned. |
of the string that it matched that is returned. |
| 1994 |
|
|
| 1995 |
If the vector is too small to hold all the captured substring offsets, |
If the vector is too small to hold all the captured substring offsets, |
| 1996 |
it is used as far as possible (up to two-thirds of its length), and the |
it is used as far as possible (up to two-thirds of its length), and the |
| 1997 |
function returns a value of zero. In particular, if the substring off- |
function returns a value of zero. In particular, if the substring off- |
| 1998 |
sets are not of interest, pcre_exec() may be called with ovector passed |
sets are not of interest, pcre_exec() may be called with ovector passed |
| 1999 |
as NULL and ovecsize as zero. However, if the pattern contains back |
as NULL and ovecsize as zero. However, if the pattern contains back |
| 2000 |
references and the ovector is not big enough to remember the related |
references and the ovector is not big enough to remember the related |
| 2001 |
substrings, PCRE has to get additional memory for use during matching. |
substrings, PCRE has to get additional memory for use during matching. |
| 2002 |
Thus it is usually advisable to supply an ovector. |
Thus it is usually advisable to supply an ovector. |
| 2003 |
|
|
| 2004 |
The pcre_info() function can be used to find out how many capturing |
The pcre_info() function can be used to find out how many capturing |
| 2005 |
subpatterns there are in a compiled pattern. The smallest size for |
subpatterns there are in a compiled pattern. The smallest size for |
| 2006 |
ovector that will allow for n captured substrings, in addition to the |
ovector that will allow for n captured substrings, in addition to the |
| 2007 |
offsets of the substring matched by the whole pattern, is (n+1)*3. |
offsets of the substring matched by the whole pattern, is (n+1)*3. |
| 2008 |
|
|
| 2009 |
It is possible for capturing subpattern number n+1 to match some part |
It is possible for capturing subpattern number n+1 to match some part |
| 2010 |
of the subject when subpattern n has not been used at all. For example, |
of the subject when subpattern n has not been used at all. For example, |
| 2011 |
if the string "abc" is matched against the pattern (a|(z))(bc) the |
if the string "abc" is matched against the pattern (a|(z))(bc) the |
| 2012 |
return from the function is 4, and subpatterns 1 and 3 are matched, but |
return from the function is 4, and subpatterns 1 and 3 are matched, but |
| 2013 |
2 is not. When this happens, both values in the offset pairs corre- |
2 is not. When this happens, both values in the offset pairs corre- |
| 2014 |
sponding to unused subpatterns are set to -1. |
sponding to unused subpatterns are set to -1. |
| 2015 |
|
|
| 2016 |
Offset values that correspond to unused subpatterns at the end of the |
Offset values that correspond to unused subpatterns at the end of the |
| 2017 |
expression are also set to -1. For example, if the string "abc" is |
expression are also set to -1. For example, if the string "abc" is |
| 2018 |
matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not |
matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not |
| 2019 |
matched. The return from the function is 2, because the highest used |
matched. The return from the function is 2, because the highest used |
| 2020 |
capturing subpattern number is 1. However, you can refer to the offsets |
capturing subpattern number is 1. However, you can refer to the offsets |
| 2021 |
for the second and third capturing subpatterns if you wish (assuming |
for the second and third capturing subpatterns if you wish (assuming |
| 2022 |
the vector is large enough, of course). |
the vector is large enough, of course). |
| 2023 |
|
|
| 2024 |
Some convenience functions are provided for extracting the captured |
Some convenience functions are provided for extracting the captured |
| 2025 |
substrings as separate strings. These are described below. |
substrings as separate strings. These are described below. |
| 2026 |
|
|
| 2027 |
Error return values from pcre_exec() |
Error return values from pcre_exec() |
| 2028 |
|
|
| 2029 |
If pcre_exec() fails, it returns a negative number. The following are |
If pcre_exec() fails, it returns a negative number. The following are |
| 2030 |
defined in the header file: |
defined in the header file: |
| 2031 |
|
|
| 2032 |
PCRE_ERROR_NOMATCH (-1) |
PCRE_ERROR_NOMATCH (-1) |
| 2035 |
|
|
| 2036 |
PCRE_ERROR_NULL (-2) |
PCRE_ERROR_NULL (-2) |
| 2037 |
|
|
| 2038 |
Either code or subject was passed as NULL, or ovector was NULL and |
Either code or subject was passed as NULL, or ovector was NULL and |
| 2039 |
ovecsize was not zero. |
ovecsize was not zero. |
| 2040 |
|
|
| 2041 |
PCRE_ERROR_BADOPTION (-3) |
PCRE_ERROR_BADOPTION (-3) |
| 2044 |
|
|
| 2045 |
PCRE_ERROR_BADMAGIC (-4) |
PCRE_ERROR_BADMAGIC (-4) |
| 2046 |
|
|
| 2047 |
PCRE stores a 4-byte "magic number" at the start of the compiled code, |
PCRE stores a 4-byte "magic number" at the start of the compiled code, |
| 2048 |
to catch the case when it is passed a junk pointer and to detect when a |
to catch the case when it is passed a junk pointer and to detect when a |
| 2049 |
pattern that was compiled in an environment of one endianness is run in |
pattern that was compiled in an environment of one endianness is run in |
| 2050 |
an environment with the other endianness. This is the error that PCRE |
an environment with the other endianness. This is the error that PCRE |
| 2051 |
gives when the magic number is not present. |
gives when the magic number is not present. |
| 2052 |
|
|
| 2053 |
PCRE_ERROR_UNKNOWN_OPCODE (-5) |
PCRE_ERROR_UNKNOWN_OPCODE (-5) |
| 2054 |
|
|
| 2055 |
While running the pattern match, an unknown item was encountered in the |
While running the pattern match, an unknown item was encountered in the |
| 2056 |
compiled pattern. This error could be caused by a bug in PCRE or by |
compiled pattern. This error could be caused by a bug in PCRE or by |
| 2057 |
overwriting of the compiled pattern. |
overwriting of the compiled pattern. |
| 2058 |
|
|
| 2059 |
PCRE_ERROR_NOMEMORY (-6) |
PCRE_ERROR_NOMEMORY (-6) |
| 2060 |
|
|
| 2061 |
If a pattern contains back references, but the ovector that is passed |
If a pattern contains back references, but the ovector that is passed |
| 2062 |
to pcre_exec() is not big enough to remember the referenced substrings, |
to pcre_exec() is not big enough to remember the referenced substrings, |
| 2063 |
PCRE gets a block of memory at the start of matching to use for this |
PCRE gets a block of memory at the start of matching to use for this |
| 2064 |
purpose. If the call via pcre_malloc() fails, this error is given. The |
purpose. If the call via pcre_malloc() fails, this error is given. The |
| 2065 |
memory is automatically freed at the end of matching. |
memory is automatically freed at the end of matching. |
| 2066 |
|
|
| 2067 |
PCRE_ERROR_NOSUBSTRING (-7) |
PCRE_ERROR_NOSUBSTRING (-7) |
| 2068 |
|
|
| 2069 |
This error is used by the pcre_copy_substring(), pcre_get_substring(), |
This error is used by the pcre_copy_substring(), pcre_get_substring(), |
| 2070 |
and pcre_get_substring_list() functions (see below). It is never |
and pcre_get_substring_list() functions (see below). It is never |
| 2071 |
returned by pcre_exec(). |
returned by pcre_exec(). |
| 2072 |
|
|
| 2073 |
PCRE_ERROR_MATCHLIMIT (-8) |
PCRE_ERROR_MATCHLIMIT (-8) |
| 2074 |
|
|
| 2075 |
The backtracking limit, as specified by the match_limit field in a |
The backtracking limit, as specified by the match_limit field in a |
| 2076 |
pcre_extra structure (or defaulted) was reached. See the description |
pcre_extra structure (or defaulted) was reached. See the description |
| 2077 |
above. |
above. |
| 2078 |
|
|
| 2079 |
PCRE_ERROR_CALLOUT (-9) |
PCRE_ERROR_CALLOUT (-9) |
| 2080 |
|
|
| 2081 |
This error is never generated by pcre_exec() itself. It is provided for |
This error is never generated by pcre_exec() itself. It is provided for |
| 2082 |
use by callout functions that want to yield a distinctive error code. |
use by callout functions that want to yield a distinctive error code. |
| 2083 |
See the pcrecallout documentation for details. |
See the pcrecallout documentation for details. |
| 2084 |
|
|
| 2085 |
PCRE_ERROR_BADUTF8 (-10) |
PCRE_ERROR_BADUTF8 (-10) |
| 2086 |
|
|
| 2087 |
A string that contains an invalid UTF-8 byte sequence was passed as a |
A string that contains an invalid UTF-8 byte sequence was passed as a |
| 2088 |
subject. |
subject. |
| 2089 |
|
|
| 2090 |
PCRE_ERROR_BADUTF8_OFFSET (-11) |
PCRE_ERROR_BADUTF8_OFFSET (-11) |
| 2091 |
|
|
| 2092 |
The UTF-8 byte sequence that was passed as a subject was valid, but the |
The UTF-8 byte sequence that was passed as a subject was valid, but the |
| 2093 |
value of startoffset did not point to the beginning of a UTF-8 charac- |
value of startoffset did not point to the beginning of a UTF-8 charac- |
| 2094 |
ter. |
ter. |
| 2095 |
|
|
| 2096 |
PCRE_ERROR_PARTIAL (-12) |
PCRE_ERROR_PARTIAL (-12) |
| 2097 |
|
|
| 2098 |
The subject string did not match, but it did match partially. See the |
The subject string did not match, but it did match partially. See the |
| 2099 |
pcrepartial documentation for details of partial matching. |
pcrepartial documentation for details of partial matching. |
| 2100 |
|
|
| 2101 |
PCRE_ERROR_BADPARTIAL (-13) |
PCRE_ERROR_BADPARTIAL (-13) |
| 2102 |
|
|
| 2103 |
The PCRE_PARTIAL option was used with a compiled pattern containing |
The PCRE_PARTIAL option was used with a compiled pattern containing |
| 2104 |
items that are not supported for partial matching. See the pcrepartial |
items that are not supported for partial matching. See the pcrepartial |
| 2105 |
documentation for details of partial matching. |
documentation for details of partial matching. |
| 2106 |
|
|
| 2107 |
PCRE_ERROR_INTERNAL (-14) |
PCRE_ERROR_INTERNAL (-14) |
| 2108 |
|
|
| 2109 |
An unexpected internal error has occurred. This error could be caused |
An unexpected internal error has occurred. This error could be caused |
| 2110 |
by a bug in PCRE or by overwriting of the compiled pattern. |
by a bug in PCRE or by overwriting of the compiled pattern. |
| 2111 |
|
|
| 2112 |
PCRE_ERROR_BADCOUNT (-15) |
PCRE_ERROR_BADCOUNT (-15) |
| 2113 |
|
|
| 2114 |
This error is given if the value of the ovecsize argument is negative. |
This error is given if the value of the ovecsize argument is negative. |
| 2115 |
|
|
| 2116 |
PCRE_ERROR_RECURSIONLIMIT (-21) |
PCRE_ERROR_RECURSIONLIMIT (-21) |
| 2117 |
|
|
| 2118 |
The internal recursion limit, as specified by the match_limit_recursion |
The internal recursion limit, as specified by the match_limit_recursion |
| 2119 |
field in a pcre_extra structure (or defaulted) was reached. See the |
field in a pcre_extra structure (or defaulted) was reached. See the |
| 2120 |
description above. |
description above. |
| 2121 |
|
|
| 2122 |
PCRE_ERROR_BADNEWLINE (-23) |
PCRE_ERROR_BADNEWLINE (-23) |
| 2139 |
int pcre_get_substring_list(const char *subject, |
int pcre_get_substring_list(const char *subject, |
| 2140 |
int *ovector, int stringcount, const char ***listptr); |
int *ovector, int stringcount, const char ***listptr); |
| 2141 |
|
|
| 2142 |
Captured substrings can be accessed directly by using the offsets |
Captured substrings can be accessed directly by using the offsets |
| 2143 |
returned by pcre_exec() in ovector. For convenience, the functions |
returned by pcre_exec() in ovector. For convenience, the functions |
| 2144 |
pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub- |
pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub- |
| 2145 |
string_list() are provided for extracting captured substrings as new, |
string_list() are provided for extracting captured substrings as new, |
| 2146 |
separate, zero-terminated strings. These functions identify substrings |
separate, zero-terminated strings. These functions identify substrings |
| 2147 |
by number. The next section describes functions for extracting named |
by number. The next section describes functions for extracting named |
| 2148 |
substrings. |
substrings. |
| 2149 |
|
|
| 2150 |
A substring that contains a binary zero is correctly extracted and has |
A substring that contains a binary zero is correctly extracted and has |
| 2151 |
a further zero added on the end, but the result is not, of course, a C |
a further zero added on the end, but the result is not, of course, a C |
| 2152 |
string. However, you can process such a string by referring to the |
string. However, you can process such a string by referring to the |
| 2153 |
length that is returned by pcre_copy_substring() and pcre_get_sub- |
length that is returned by pcre_copy_substring() and pcre_get_sub- |
| 2154 |
string(). Unfortunately, the interface to pcre_get_substring_list() is |
string(). Unfortunately, the interface to pcre_get_substring_list() is |
| 2155 |
not adequate for handling strings containing binary zeros, because the |
not adequate for handling strings containing binary zeros, because the |
| 2156 |
end of the final string is not independently indicated. |
end of the final string is not independently indicated. |
| 2157 |
|
|
| 2158 |
The first three arguments are the same for all three of these func- |
The first three arguments are the same for all three of these func- |
| 2159 |
tions: subject is the subject string that has just been successfully |
tions: subject is the subject string that has just been successfully |
| 2160 |
matched, ovector is a pointer to the vector of integer offsets that was |
matched, ovector is a pointer to the vector of integer offsets that was |
| 2161 |
passed to pcre_exec(), and stringcount is the number of substrings that |
passed to pcre_exec(), and stringcount is the number of substrings that |
| 2162 |
were captured by the match, including the substring that matched the |
were captured by the match, including the substring that matched the |
| 2163 |
entire regular expression. This is the value returned by pcre_exec() if |
entire regular expression. This is the value returned by pcre_exec() if |
| 2164 |
it is greater than zero. If pcre_exec() returned zero, indicating that |
it is greater than zero. If pcre_exec() returned zero, indicating that |
| 2165 |
it ran out of space in ovector, the value passed as stringcount should |
it ran out of space in ovector, the value passed as stringcount should |
| 2166 |
be the number of elements in the vector divided by three. |
be the number of elements in the vector divided by three. |
| 2167 |
|
|
| 2168 |
The functions pcre_copy_substring() and pcre_get_substring() extract a |
The functions pcre_copy_substring() and pcre_get_substring() extract a |
| 2169 |
single substring, whose number is given as stringnumber. A value of |
single substring, whose number is given as stringnumber. A value of |
| 2170 |
zero extracts the substring that matched the entire pattern, whereas |
zero extracts the substring that matched the entire pattern, whereas |
| 2171 |
higher values extract the captured substrings. For pcre_copy_sub- |
higher values extract the captured substrings. For pcre_copy_sub- |
| 2172 |
string(), the string is placed in buffer, whose length is given by |
string(), the string is placed in buffer, whose length is given by |
| 2173 |
buffersize, while for pcre_get_substring() a new block of memory is |
buffersize, while for pcre_get_substring() a new block of memory is |
| 2174 |
obtained via pcre_malloc, and its address is returned via stringptr. |
obtained via pcre_malloc, and its address is returned via stringptr. |
| 2175 |
The yield of the function is the length of the string, not including |
The yield of the function is the length of the string, not including |
| 2176 |
the terminating zero, or one of these error codes: |
the terminating zero, or one of these error codes: |
| 2177 |
|
|
| 2178 |
PCRE_ERROR_NOMEMORY (-6) |
PCRE_ERROR_NOMEMORY (-6) |
| 2179 |
|
|
| 2180 |
The buffer was too small for pcre_copy_substring(), or the attempt to |
The buffer was too small for pcre_copy_substring(), or the attempt to |
| 2181 |
get memory failed for pcre_get_substring(). |
get memory failed for pcre_get_substring(). |
| 2182 |
|
|
| 2183 |
PCRE_ERROR_NOSUBSTRING (-7) |
PCRE_ERROR_NOSUBSTRING (-7) |
| 2184 |
|
|
| 2185 |
There is no substring whose number is stringnumber. |
There is no substring whose number is stringnumber. |
| 2186 |
|
|
| 2187 |
The pcre_get_substring_list() function extracts all available sub- |
The pcre_get_substring_list() function extracts all available sub- |
| 2188 |
strings and builds a list of pointers to them. All this is done in a |
strings and builds a list of pointers to them. All this is done in a |
| 2189 |
single block of memory that is obtained via pcre_malloc. The address of |
single block of memory that is obtained via pcre_malloc. The address of |
| 2190 |
the memory block is returned via listptr, which is also the start of |
the memory block is returned via listptr, which is also the start of |
| 2191 |
the list of string pointers. The end of the list is marked by a NULL |
the list of string pointers. The end of the list is marked by a NULL |
| 2192 |
pointer. The yield of the function is zero if all went well, or the |
pointer. The yield of the function is zero if all went well, or the |
| 2193 |
error code |
error code |
| 2194 |
|
|
| 2195 |
PCRE_ERROR_NOMEMORY (-6) |
PCRE_ERROR_NOMEMORY (-6) |
| 2196 |
|
|
| 2197 |
if the attempt to get the memory block failed. |
if the attempt to get the memory block failed. |
| 2198 |
|
|
| 2199 |
When any of these functions encounter a substring that is unset, which |
When any of these functions encounter a substring that is unset, which |
| 2200 |
can happen when capturing subpattern number n+1 matches some part of |
can happen when capturing subpattern number n+1 matches some part of |
| 2201 |
the subject, but subpattern n has not been used at all, they return an |
the subject, but subpattern n has not been used at all, they return an |
| 2202 |
empty string. This can be distinguished from a genuine zero-length sub- |
empty string. This can be distinguished from a genuine zero-length sub- |
| 2203 |
string by inspecting the appropriate offset in ovector, which is nega- |
string by inspecting the appropriate offset in ovector, which is nega- |
| 2204 |
tive for unset substrings. |
tive for unset substrings. |
| 2205 |
|
|
| 2206 |
The two convenience functions pcre_free_substring() and pcre_free_sub- |
The two convenience functions pcre_free_substring() and pcre_free_sub- |
| 2207 |
string_list() can be used to free the memory returned by a previous |
string_list() can be used to free the memory returned by a previous |
| 2208 |
call of pcre_get_substring() or pcre_get_substring_list(), respec- |
call of pcre_get_substring() or pcre_get_substring_list(), respec- |
| 2209 |
tively. They do nothing more than call the function pointed to by |
tively. They do nothing more than call the function pointed to by |
| 2210 |
pcre_free, which of course could be called directly from a C program. |
pcre_free, which of course could be called directly from a C program. |
| 2211 |
However, PCRE is used in some situations where it is linked via a spe- |
However, PCRE is used in some situations where it is linked via a spe- |
| 2212 |
cial interface to another programming language that cannot use |
cial interface to another programming language that cannot use |
| 2213 |
pcre_free directly; it is for these cases that the functions are pro- |
pcre_free directly; it is for these cases that the functions are pro- |
| 2214 |
vided. |
vided. |
| 2215 |
|
|
| 2216 |
|
|
| 2229 |
int stringcount, const char *stringname, |
int stringcount, const char *stringname, |
| 2230 |
const char **stringptr); |
const char **stringptr); |
| 2231 |
|
|
| 2232 |
To extract a substring by name, you first have to find associated num- |
To extract a substring by name, you first have to find associated num- |
| 2233 |
ber. For example, for this pattern |
ber. For example, for this pattern |
| 2234 |
|
|
| 2235 |
(a+)b(?<xxx>\d+)... |
(a+)b(?<xxx>\d+)... |
| 2238 |
be unique (PCRE_DUPNAMES was not set), you can find the number from the |
be unique (PCRE_DUPNAMES was not set), you can find the number from the |
| 2239 |
name by calling pcre_get_stringnumber(). The first argument is the com- |
name by calling pcre_get_stringnumber(). The first argument is the com- |
| 2240 |
piled pattern, and the second is the name. The yield of the function is |
piled pattern, and the second is the name. The yield of the function is |
| 2241 |
the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no |
the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no |
| 2242 |
subpattern of that name. |
subpattern of that name. |
| 2243 |
|
|
| 2244 |
Given the number, you can extract the substring directly, or use one of |
Given the number, you can extract the substring directly, or use one of |
| 2245 |
the functions described in the previous section. For convenience, there |
the functions described in the previous section. For convenience, there |
| 2246 |
are also two functions that do the whole job. |
are also two functions that do the whole job. |
| 2247 |
|
|
| 2248 |
Most of the arguments of pcre_copy_named_substring() and |
Most of the arguments of pcre_copy_named_substring() and |
| 2249 |
pcre_get_named_substring() are the same as those for the similarly |
pcre_get_named_substring() are the same as those for the similarly |
| 2250 |
named functions that extract by number. As these are described in the |
named functions that extract by number. As these are described in the |
| 2251 |
previous section, they are not re-described here. There are just two |
previous section, they are not re-described here. There are just two |
| 2252 |
differences: |
differences: |
| 2253 |
|
|
| 2254 |
First, instead of a substring number, a substring name is given. Sec- |
First, instead of a substring number, a substring name is given. Sec- |
| 2255 |
ond, there is an extra argument, given at the start, which is a pointer |
ond, there is an extra argument, given at the start, which is a pointer |
| 2256 |
to the compiled pattern. This is needed in order to gain access to the |
to the compiled pattern. This is needed in order to gain access to the |
| 2257 |
name-to-number translation table. |
name-to-number translation table. |
| 2258 |
|
|
| 2259 |
These functions call pcre_get_stringnumber(), and if it succeeds, they |
These functions call pcre_get_stringnumber(), and if it succeeds, they |
| 2260 |
then call pcre_copy_substring() or pcre_get_substring(), as appropri- |
then call pcre_copy_substring() or pcre_get_substring(), as appropri- |
| 2261 |
ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate names, the |
ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate names, the |
| 2262 |
behaviour may not be what you want (see the next section). |
behaviour may not be what you want (see the next section). |
| 2263 |
|
|
| 2264 |
|
|
| 2267 |
int pcre_get_stringtable_entries(const pcre *code, |
int pcre_get_stringtable_entries(const pcre *code, |
| 2268 |
const char *name, char **first, char **last); |
const char *name, char **first, char **last); |
| 2269 |
|
|
| 2270 |
When a pattern is compiled with the PCRE_DUPNAMES option, names for |
When a pattern is compiled with the PCRE_DUPNAMES option, names for |
| 2271 |
subpatterns are not required to be unique. Normally, patterns with |
subpatterns are not required to be unique. Normally, patterns with |
| 2272 |
duplicate names are such that in any one match, only one of the named |
duplicate names are such that in any one match, only one of the named |
| 2273 |
subpatterns participates. An example is shown in the pcrepattern docu- |
subpatterns participates. An example is shown in the pcrepattern docu- |
| 2274 |
mentation. |
mentation. |
| 2275 |
|
|
| 2276 |
When duplicates are present, pcre_copy_named_substring() and |
When duplicates are present, pcre_copy_named_substring() and |
| 2277 |
pcre_get_named_substring() return the first substring corresponding to |
pcre_get_named_substring() return the first substring corresponding to |
| 2278 |
the given name that is set. If none are set, PCRE_ERROR_NOSUBSTRING |
the given name that is set. If none are set, PCRE_ERROR_NOSUBSTRING |
| 2279 |
(-7) is returned; no data is returned. The pcre_get_stringnumber() |
(-7) is returned; no data is returned. The pcre_get_stringnumber() |
| 2280 |
function returns one of the numbers that are associated with the name, |
function returns one of the numbers that are associated with the name, |
| 2281 |
but it is not defined which it is. |
but it is not defined which it is. |
| 2282 |
|
|
| 2283 |
If you want to get full details of all captured substrings for a given |
If you want to get full details of all captured substrings for a given |
| 2284 |
name, you must use the pcre_get_stringtable_entries() function. The |
name, you must use the pcre_get_stringtable_entries() function. The |
| 2285 |
first argument is the compiled pattern, and the second is the name. The |
first argument is the compiled pattern, and the second is the name. The |
| 2286 |
third and fourth are pointers to variables which are updated by the |
third and fourth are pointers to variables which are updated by the |
| 2287 |
function. After it has run, they point to the first and last entries in |
function. After it has run, they point to the first and last entries in |
| 2288 |
the name-to-number table for the given name. The function itself |
the name-to-number table for the given name. The function itself |
| 2289 |
returns the length of each entry, or PCRE_ERROR_NOSUBSTRING (-7) if |
returns the length of each entry, or PCRE_ERROR_NOSUBSTRING (-7) if |
| 2290 |
there are none. The format of the table is described above in the sec- |
there are none. The format of the table is described above in the sec- |
| 2291 |
tion entitled Information about a pattern. Given all the relevant |
tion entitled Information about a pattern. Given all the relevant |
| 2292 |
entries for the name, you can extract each of their numbers, and hence |
entries for the name, you can extract each of their numbers, and hence |
| 2293 |
the captured data, if any. |
the captured data, if any. |
| 2294 |
|
|
| 2295 |
|
|
| 2296 |
FINDING ALL POSSIBLE MATCHES |
FINDING ALL POSSIBLE MATCHES |
| 2297 |
|
|
| 2298 |
The traditional matching function uses a similar algorithm to Perl, |
The traditional matching function uses a similar algorithm to Perl, |
| 2299 |
which stops when it finds the first match, starting at a given point in |
which stops when it finds the first match, starting at a given point in |
| 2300 |
the subject. If you want to find all possible matches, or the longest |
the subject. If you want to find all possible matches, or the longest |
| 2301 |
possible match, consider using the alternative matching function (see |
possible match, consider using the alternative matching function (see |
| 2302 |
below) instead. If you cannot use the alternative function, but still |
below) instead. If you cannot use the alternative function, but still |
| 2303 |
need to find all possible matches, you can kludge it up by making use |
need to find all possible matches, you can kludge it up by making use |
| 2304 |
of the callout facility, which is described in the pcrecallout documen- |
of the callout facility, which is described in the pcrecallout documen- |
| 2305 |
tation. |
tation. |
| 2306 |
|
|
| 2307 |
What you have to do is to insert a callout right at the end of the pat- |
What you have to do is to insert a callout right at the end of the pat- |
| 2308 |
tern. When your callout function is called, extract and save the cur- |
tern. When your callout function is called, extract and save the cur- |
| 2309 |
rent matched substring. Then return 1, which forces pcre_exec() to |
rent matched substring. Then return 1, which forces pcre_exec() to |
| 2310 |
backtrack and try other alternatives. Ultimately, when it runs out of |
backtrack and try other alternatives. Ultimately, when it runs out of |
| 2311 |
matches, pcre_exec() will yield PCRE_ERROR_NOMATCH. |
matches, pcre_exec() will yield PCRE_ERROR_NOMATCH. |
| 2312 |
|
|
| 2313 |
|
|
| 2318 |
int options, int *ovector, int ovecsize, |
int options, int *ovector, int ovecsize, |
| 2319 |
int *workspace, int wscount); |
int *workspace, int wscount); |
| 2320 |
|
|
| 2321 |
The function pcre_dfa_exec() is called to match a subject string |
The function pcre_dfa_exec() is called to match a subject string |
| 2322 |
against a compiled pattern, using a matching algorithm that scans the |
against a compiled pattern, using a matching algorithm that scans the |
| 2323 |
subject string just once, and does not backtrack. This has different |
subject string just once, and does not backtrack. This has different |
| 2324 |
characteristics to the normal algorithm, and is not compatible with |
characteristics to the normal algorithm, and is not compatible with |
| 2325 |
Perl. Some of the features of PCRE patterns are not supported. Never- |
Perl. Some of the features of PCRE patterns are not supported. Never- |
| 2326 |
theless, there are times when this kind of matching can be useful. For |
theless, there are times when this kind of matching can be useful. For |
| 2327 |
a discussion of the two matching algorithms, see the pcrematching docu- |
a discussion of the two matching algorithms, see the pcrematching docu- |
| 2328 |
mentation. |
mentation. |
| 2329 |
|
|
| 2330 |
The arguments for the pcre_dfa_exec() function are the same as for |
The arguments for the pcre_dfa_exec() function are the same as for |
| 2331 |
pcre_exec(), plus two extras. The ovector argument is used in a differ- |
pcre_exec(), plus two extras. The ovector argument is used in a differ- |
| 2332 |
ent way, and this is described below. The other common arguments are |
ent way, and this is described below. The other common arguments are |
| 2333 |
used in the same way as for pcre_exec(), so their description is not |
used in the same way as for pcre_exec(), so their description is not |
| 2334 |
repeated here. |
repeated here. |
| 2335 |
|
|
| 2336 |
The two additional arguments provide workspace for the function. The |
The two additional arguments provide workspace for the function. The |
| 2337 |
workspace vector should contain at least 20 elements. It is used for |
workspace vector should contain at least 20 elements. It is used for |
| 2338 |
keeping track of multiple paths through the pattern tree. More |
keeping track of multiple paths through the pattern tree. More |
| 2339 |
workspace will be needed for patterns and subjects where there are a |
workspace will be needed for patterns and subjects where there are a |
| 2340 |
lot of potential matches. |
lot of potential matches. |
| 2341 |
|
|
| 2342 |
Here is an example of a simple call to pcre_dfa_exec(): |
Here is an example of a simple call to pcre_dfa_exec(): |
| 2358 |
|
|
| 2359 |
Option bits for pcre_dfa_exec() |
Option bits for pcre_dfa_exec() |
| 2360 |
|
|
| 2361 |
The unused bits of the options argument for pcre_dfa_exec() must be |
The unused bits of the options argument for pcre_dfa_exec() must be |
| 2362 |
zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEW- |
zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEW- |
| 2363 |
LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK, |
LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK, |
| 2364 |
PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last |
PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last |
| 2365 |
three of these are the same as for pcre_exec(), so their description is |
three of these are the same as for pcre_exec(), so their description is |
| 2366 |
not repeated here. |
not repeated here. |
| 2367 |
|
|
| 2368 |
PCRE_PARTIAL |
PCRE_PARTIAL |
| 2369 |
|
|
| 2370 |
This has the same general effect as it does for pcre_exec(), but the |
This has the same general effect as it does for pcre_exec(), but the |
| 2371 |
details are slightly different. When PCRE_PARTIAL is set for |
details are slightly different. When PCRE_PARTIAL is set for |
| 2372 |
pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is converted into |
pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is converted into |
| 2373 |
PCRE_ERROR_PARTIAL if the end of the subject is reached, there have |
PCRE_ERROR_PARTIAL if the end of the subject is reached, there have |
| 2374 |
been no complete matches, but there is still at least one matching pos- |
been no complete matches, but there is still at least one matching pos- |
| 2375 |
sibility. The portion of the string that provided the partial match is |
sibility. The portion of the string that provided the partial match is |
| 2376 |
set as the first matching string. |
set as the first matching string. |
| 2377 |
|
|
| 2378 |
PCRE_DFA_SHORTEST |
PCRE_DFA_SHORTEST |
| 2379 |
|
|
| 2380 |
Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to |
Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to |
| 2381 |
stop as soon as it has found one match. Because of the way the alterna- |
stop as soon as it has found one match. Because of the way the alterna- |
| 2382 |
tive algorithm works, this is necessarily the shortest possible match |
tive algorithm works, this is necessarily the shortest possible match |
| 2383 |
at the first possible matching point in the subject string. |
at the first possible matching point in the subject string. |
| 2384 |
|
|
| 2385 |
PCRE_DFA_RESTART |
PCRE_DFA_RESTART |
| 2386 |
|
|
| 2387 |
When pcre_dfa_exec() is called with the PCRE_PARTIAL option, and |
When pcre_dfa_exec() is called with the PCRE_PARTIAL option, and |
| 2388 |
returns a partial match, it is possible to call it again, with addi- |
returns a partial match, it is possible to call it again, with addi- |
| 2389 |
tional subject characters, and have it continue with the same match. |
tional subject characters, and have it continue with the same match. |
| 2390 |
The PCRE_DFA_RESTART option requests this action; when it is set, the |
The PCRE_DFA_RESTART option requests this action; when it is set, the |
| 2391 |
workspace and wscount options must reference the same vector as before |
workspace and wscount options must reference the same vector as before |
| 2392 |
because data about the match so far is left in them after a partial |
because data about the match so far is left in them after a partial |
| 2393 |
match. There is more discussion of this facility in the pcrepartial |
match. There is more discussion of this facility in the pcrepartial |
| 2394 |
documentation. |
documentation. |
| 2395 |
|
|
| 2396 |
Successful returns from pcre_dfa_exec() |
Successful returns from pcre_dfa_exec() |
| 2397 |
|
|
| 2398 |
When pcre_dfa_exec() succeeds, it may have matched more than one sub- |
When pcre_dfa_exec() succeeds, it may have matched more than one sub- |
| 2399 |
string in the subject. Note, however, that all the matches from one run |
string in the subject. Note, however, that all the matches from one run |
| 2400 |
of the function start at the same point in the subject. The shorter |
of the function start at the same point in the subject. The shorter |
| 2401 |
matches are all initial substrings of the longer matches. For example, |
matches are all initial substrings of the longer matches. For example, |
| 2402 |
if the pattern |
if the pattern |
| 2403 |
|
|
| 2404 |
<.*> |
<.*> |
| 2413 |
<something> <something else> |
<something> <something else> |
| 2414 |
<something> <something else> <something further> |
<something> <something else> <something further> |
| 2415 |
|
|
| 2416 |
On success, the yield of the function is a number greater than zero, |
On success, the yield of the function is a number greater than zero, |
| 2417 |
which is the number of matched substrings. The substrings themselves |
which is the number of matched substrings. The substrings themselves |
| 2418 |
are returned in ovector. Each string uses two elements; the first is |
are returned in ovector. Each string uses two elements; the first is |
| 2419 |
the offset to the start, and the second is the offset to the end. In |
the offset to the start, and the second is the offset to the end. In |
| 2420 |
fact, all the strings have the same start offset. (Space could have |
fact, all the strings have the same start offset. (Space could have |
| 2421 |
been saved by giving this only once, but it was decided to retain some |
been saved by giving this only once, but it was decided to retain some |
| 2422 |
compatibility with the way pcre_exec() returns data, even though the |
compatibility with the way pcre_exec() returns data, even though the |
| 2423 |
meaning of the strings is different.) |
meaning of the strings is different.) |
| 2424 |
|
|
| 2425 |
The strings are returned in reverse order of length; that is, the long- |
The strings are returned in reverse order of length; that is, the long- |
| 2426 |
est matching string is given first. If there were too many matches to |
est matching string is given first. If there were too many matches to |
| 2427 |
fit into ovector, the yield of the function is zero, and the vector is |
fit into ovector, the yield of the function is zero, and the vector is |
| 2428 |
filled with the longest matches. |
filled with the longest matches. |
| 2429 |
|
|
| 2430 |
Error returns from pcre_dfa_exec() |
Error returns from pcre_dfa_exec() |
| 2431 |
|
|
| 2432 |
The pcre_dfa_exec() function returns a negative number when it fails. |
The pcre_dfa_exec() function returns a negative number when it fails. |
| 2433 |
Many of the errors are the same as for pcre_exec(), and these are |
Many of the errors are the same as for pcre_exec(), and these are |
| 2434 |
described above. There are in addition the following errors that are |
described above. There are in addition the following errors that are |
| 2435 |
specific to pcre_dfa_exec(): |
specific to pcre_dfa_exec(): |
| 2436 |
|
|
| 2437 |
PCRE_ERROR_DFA_UITEM (-16) |
PCRE_ERROR_DFA_UITEM (-16) |
| 2438 |
|
|
| 2439 |
This return is given if pcre_dfa_exec() encounters an item in the pat- |
This return is given if pcre_dfa_exec() encounters an item in the pat- |
| 2440 |
tern that it does not support, for instance, the use of \C or a back |
tern that it does not support, for instance, the use of \C or a back |
| 2441 |
reference. |
reference. |
| 2442 |
|
|
| 2443 |
PCRE_ERROR_DFA_UCOND (-17) |
PCRE_ERROR_DFA_UCOND (-17) |
| 2444 |
|
|
| 2445 |
This return is given if pcre_dfa_exec() encounters a condition item |
This return is given if pcre_dfa_exec() encounters a condition item |
| 2446 |
that uses a back reference for the condition, or a test for recursion |
that uses a back reference for the condition, or a test for recursion |
| 2447 |
in a specific group. These are not supported. |
in a specific group. These are not supported. |
| 2448 |
|
|
| 2449 |
PCRE_ERROR_DFA_UMLIMIT (-18) |
PCRE_ERROR_DFA_UMLIMIT (-18) |
| 2450 |
|
|
| 2451 |
This return is given if pcre_dfa_exec() is called with an extra block |
This return is given if pcre_dfa_exec() is called with an extra block |
| 2452 |
that contains a setting of the match_limit field. This is not supported |
that contains a setting of the match_limit field. This is not supported |
| 2453 |
(it is meaningless). |
(it is meaningless). |
| 2454 |
|
|
| 2455 |
PCRE_ERROR_DFA_WSSIZE (-19) |
PCRE_ERROR_DFA_WSSIZE (-19) |
| 2456 |
|
|
| 2457 |
This return is given if pcre_dfa_exec() runs out of space in the |
This return is given if pcre_dfa_exec() runs out of space in the |
| 2458 |
workspace vector. |
workspace vector. |
| 2459 |
|
|
| 2460 |
PCRE_ERROR_DFA_RECURSE (-20) |
PCRE_ERROR_DFA_RECURSE (-20) |
| 2461 |
|
|
| 2462 |
When a recursive subpattern is processed, the matching function calls |
When a recursive subpattern is processed, the matching function calls |
| 2463 |
itself recursively, using private vectors for ovector and workspace. |
itself recursively, using private vectors for ovector and workspace. |
| 2464 |
This error is given if the output vector is not large enough. This |
This error is given if the output vector is not large enough. This |
| 2465 |
should be extremely rare, as a vector of size 1000 is used. |
should be extremely rare, as a vector of size 1000 is used. |
| 2466 |
|
|
| 2467 |
|
|
| 2468 |
SEE ALSO |
SEE ALSO |
| 2469 |
|
|
| 2470 |
pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar- |
pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar- |
| 2471 |
tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3). |
tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3). |
| 2472 |
|
|
| 2473 |
|
|
| 2474 |
AUTHOR |
AUTHOR |
| 2480 |
|
|
| 2481 |
REVISION |
REVISION |
| 2482 |
|
|
| 2483 |
Last updated: 09 August 2007 |
Last updated: 21 August 2007 |
| 2484 |
Copyright (c) 1997-2007 University of Cambridge. |
Copyright (c) 1997-2007 University of Cambridge. |
| 2485 |
------------------------------------------------------------------------------ |
------------------------------------------------------------------------------ |
| 2486 |
|
|
| 2826 |
discussed in the pcrematching page. |
discussed in the pcrematching page. |
| 2827 |
|
|
| 2828 |
|
|
| 2829 |
|
NEWLINE CONVENTIONS |
| 2830 |
|
|
| 2831 |
|
PCRE supports five different conventions for indicating line breaks in |
| 2832 |
|
strings: a single CR (carriage return) character, a single LF (line- |
| 2833 |
|
feed) character, the two-character sequence CRLF, any of the three pre- |
| 2834 |
|
ceding, or any Unicode newline sequence. The pcreapi page has further |
| 2835 |
|
discussion about newlines, and shows how to set the newline convention |
| 2836 |
|
in the options arguments for the compiling and matching functions. |
| 2837 |
|
|
| 2838 |
|
It is also possible to specify a newline convention by starting a pat- |
| 2839 |
|
tern string with one of the following five sequences: |
| 2840 |
|
|
| 2841 |
|
(*CR) carriage return |
| 2842 |
|
(*LF) linefeed |
| 2843 |
|
(*CRLF) carriage return, followed by linefeed |
| 2844 |
|
(*ANYCRLF) any of the three above |
| 2845 |
|
(*ANY) all Unicode newline sequences |
| 2846 |
|
|
| 2847 |
|
These override the default and the options given to pcre_compile(). For |
| 2848 |
|
example, on a Unix system where LF is the default newline sequence, the |
| 2849 |
|
pattern |
| 2850 |
|
|
| 2851 |
|
(*CR)a.b |
| 2852 |
|
|
| 2853 |
|
changes the convention to CR. That pattern matches "a\nb" because LF is |
| 2854 |
|
no longer a newline. Note that these special settings, which are not |
| 2855 |
|
Perl-compatible, are recognized only at the very start of a pattern, |
| 2856 |
|
and that they must be in upper case. |
| 2857 |
|
|
| 2858 |
|
|
| 2859 |
CHARACTERS AND METACHARACTERS |
CHARACTERS AND METACHARACTERS |
| 2860 |
|
|
| 2861 |
A regular expression is a pattern that is matched against a subject |
A regular expression is a pattern that is matched against a subject |
| 2862 |
string from left to right. Most characters stand for themselves in a |
string from left to right. Most characters stand for themselves in a |
| 2863 |
pattern, and match the corresponding characters in the subject. As a |
pattern, and match the corresponding characters in the subject. As a |
| 2864 |
trivial example, the pattern |
trivial example, the pattern |
| 2865 |
|
|
| 2866 |
The quick brown fox |
The quick brown fox |
| 2867 |
|
|
| 2868 |
matches a portion of a subject string that is identical to itself. When |
matches a portion of a subject string that is identical to itself. When |
| 2869 |
caseless matching is specified (the PCRE_CASELESS option), letters are |
caseless matching is specified (the PCRE_CASELESS option), letters are |
| 2870 |
matched independently of case. In UTF-8 mode, PCRE always understands |
matched independently of case. In UTF-8 mode, PCRE always understands |
| 2871 |
the concept of case for characters whose values are less than 128, so |
the concept of case for characters whose values are less than 128, so |
| 2872 |
caseless matching is always possible. For characters with higher val- |
caseless matching is always possible. For characters with higher val- |
| 2873 |
ues, the concept of case is supported if PCRE is compiled with Unicode |
ues, the concept of case is supported if PCRE is compiled with Unicode |
| 2874 |
property support, but not otherwise. If you want to use caseless |
property support, but not otherwise. If you want to use caseless |
| 2875 |
matching for characters 128 and above, you must ensure that PCRE is |
matching for characters 128 and above, you must ensure that PCRE is |
| 2876 |
compiled with Unicode property support as well as with UTF-8 support. |
compiled with Unicode property support as well as with UTF-8 support. |
| 2877 |
|
|
| 2878 |
The power of regular expressions comes from the ability to include |
The power of regular expressions comes from the ability to include |
| 2879 |
alternatives and repetitions in the pattern. These are encoded in the |
alternatives and repetitions in the pattern. These are encoded in the |
| 2880 |
pattern by the use of metacharacters, which do not stand for themselves |
pattern by the use of metacharacters, which do not stand for themselves |
| 2881 |
but instead are interpreted in some special way. |
but instead are interpreted in some special way. |
| 2882 |
|
|
| 2883 |
There are two different sets of metacharacters: those that are recog- |
There are two different sets of metacharacters: those that are recog- |
| 2884 |
nized anywhere in the pattern except within square brackets, and those |
nized anywhere in the pattern except within square brackets, and those |
| 2885 |
that are recognized within square brackets. Outside square brackets, |
that are recognized within square brackets. Outside square brackets, |
| 2886 |
the metacharacters are as follows: |
the metacharacters are as follows: |
| 2887 |
|
|
| 2888 |
\ general escape character with several uses |
\ general escape character with several uses |
| 2901 |
also "possessive quantifier" |
also "possessive quantifier" |
| 2902 |
{ start min/max quantifier |
{ start min/max quantifier |
| 2903 |
|
|
| 2904 |
Part of a pattern that is in square brackets is called a "character |
Part of a pattern that is in square brackets is called a "character |
| 2905 |
class". In a character class the only metacharacters are: |
class". In a character class the only metacharacters are: |
| 2906 |
|
|
| 2907 |
\ general escape character |
\ general escape character |
| 2911 |
syntax) |
syntax) |
| 2912 |
] terminates the character class |
] terminates the character class |
| 2913 |
|
|
| 2914 |
The following sections describe the use of each of the metacharacters. |
The following sections describe the use of each of the metacharacters. |
| 2915 |
|
|
| 2916 |
|
|
| 2917 |
BACKSLASH |
BACKSLASH |
| 2918 |
|
|
| 2919 |
The backslash character has several uses. Firstly, if it is followed by |
The backslash character has several uses. Firstly, if it is followed by |
| 2920 |
a non-alphanumeric character, it takes away any special meaning that |
a non-alphanumeric character, it takes away any special meaning that |
| 2921 |
character may have. This use of backslash as an escape character |
character may have. This use of backslash as an escape character |
| 2922 |
applies both inside and outside character classes. |
applies both inside and outside character classes. |
| 2923 |
|
|
| 2924 |
For example, if you want to match a * character, you write \* in the |
For example, if you want to match a * character, you write \* in the |
| 2925 |
pattern. This escaping action applies whether or not the following |
pattern. This escaping action applies whether or not the following |
| 2926 |
character would otherwise be interpreted as a metacharacter, so it is |
character would otherwise be interpreted as a metacharacter, so it is |
| 2927 |
always safe to precede a non-alphanumeric with backslash to specify |
always safe to precede a non-alphanumeric with backslash to specify |
| 2928 |
that it stands for itself. In particular, if you want to match a back- |
that it stands for itself. In particular, if you want to match a back- |
| 2929 |
slash, you write \\. |
slash, you write \\. |
| 2930 |
|
|
| 2931 |
If a pattern is compiled with the PCRE_EXTENDED option, whitespace in |
If a pattern is compiled with the PCRE_EXTENDED option, whitespace in |
| 2932 |
the pattern (other than in a character class) and characters between a |
the pattern (other than in a character class) and characters between a |
| 2933 |
# outside a character class and the next newline are ignored. An escap- |
# outside a character class and the next newline are ignored. An escap- |
| 2934 |
ing backslash can be used to include a whitespace or # character as |
ing backslash can be used to include a whitespace or # character as |
| 2935 |
part of the pattern. |
part of the pattern. |
| 2936 |
|
|
| 2937 |
If you want to remove the special meaning from a sequence of charac- |
If you want to remove the special meaning from a sequence of charac- |
| 2938 |
ters, you can do so by putting them between \Q and \E. This is differ- |
ters, you can do so by putting them between \Q and \E. This is differ- |
| 2939 |
ent from Perl in that $ and @ are handled as literals in \Q...\E |
ent from Perl in that $ and @ are handled as literals in \Q...\E |
| 2940 |
sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- |
sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- |
| 2941 |
tion. Note the following examples: |
tion. Note the following examples: |
| 2942 |
|
|
| 2943 |
Pattern PCRE matches Perl matches |
Pattern PCRE matches Perl matches |
| 2947 |
\Qabc\$xyz\E abc\$xyz abc\$xyz |
\Qabc\$xyz\E abc\$xyz abc\$xyz |
| 2948 |
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
| 2949 |
|
|
| 2950 |
The \Q...\E sequence is recognized both inside and outside character |
The \Q...\E sequence is recognized both inside and outside character |
| 2951 |
classes. |
classes. |
| 2952 |
|
|
| 2953 |
Non-printing characters |
Non-printing characters |
| 2954 |
|
|
| 2955 |
A second use of backslash provides a way of encoding non-printing char- |
A second use of backslash provides a way of encoding non-printing char- |
| 2956 |
acters in patterns in a visible manner. There is no restriction on the |
acters in patterns in a visible manner. There is no restriction on the |
| 2957 |
appearance of non-printing characters, apart from the binary zero that |
appearance of non-printing characters, apart from the binary zero that |
| 2958 |
terminates a pattern, but when a pattern is being prepared by text |
terminates a pattern, but when a pattern is being prepared by text |
| 2959 |
editing, it is usually easier to use one of the following escape |
editing, it is usually easier to use one of the following escape |
| 2960 |
sequences than the binary character it represents: |
sequences than the binary character it represents: |
| 2961 |
|
|
| 2962 |
\a alarm, that is, the BEL character (hex 07) |
\a alarm, that is, the BEL character (hex 07) |
| 2963 |
\cx "control-x", where x is any character |
\cx "control-x", where x is any character |
| 2964 |
\e escape (hex 1B) |
\e escape (hex 1B) |
| 2965 |
\f formfeed (hex 0C) |
\f formfeed (hex 0C) |
| 2966 |
\n newline (hex 0A) |
\n linefeed (hex 0A) |
| 2967 |
\r carriage return (hex 0D) |
\r carriage return (hex 0D) |
| 2968 |
\t tab (hex 09) |
\t tab (hex 09) |
| 2969 |
\ddd character with octal code ddd, or backreference |
\ddd character with octal code ddd, or backreference |
| 2970 |
\xhh character with hex code hh |
\xhh character with hex code hh |
| 2971 |
\x{hhh..} character with hex code hhh.. |
\x{hhh..} character with hex code hhh.. |
| 2972 |
|
|
| 2973 |
The precise effect of \cx is as follows: if x is a lower case letter, |
The precise effect of \cx is as follows: if x is a lower case letter, |
| 2974 |
it is converted to upper case. Then bit 6 of the character (hex 40) is |
it is converted to upper case. Then bit 6 of the character (hex 40) is |
| 2975 |
inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; |
inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; |
| 2976 |
becomes hex 7B. |
becomes hex 7B. |
| 2977 |
|
|
| 2978 |
After \x, from zero to two hexadecimal digits are read (letters can be |
After \x, from zero to two hexadecimal digits are read (letters can be |
| 2979 |
in upper or lower case). Any number of hexadecimal digits may appear |
in upper or lower case). Any number of hexadecimal digits may appear |
| 2980 |
between \x{ and }, but the value of the character code must be less |
between \x{ and }, but the value of the character code must be less |
| 2981 |
than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode. That is, |
than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode. That is, |
| 2982 |
the maximum value in hexadecimal is 7FFFFFFF. Note that this is bigger |
the maximum value in hexadecimal is 7FFFFFFF. Note that this is bigger |
| 2983 |
than the largest Unicode code point, which is 10FFFF. |
than the largest Unicode code point, which is 10FFFF. |
| 2984 |
|
|
| 2985 |
If characters other than hexadecimal digits appear between \x{ and }, |
If characters other than hexadecimal digits appear between \x{ and }, |
| 2986 |
or if there is no terminating }, this form of escape is not recognized. |
or if there is no terminating }, this form of escape is not recognized. |
| 2987 |
Instead, the initial \x will be interpreted as a basic hexadecimal |
Instead, the initial \x will be interpreted as a basic hexadecimal |
| 2988 |
escape, with no following digits, giving a character whose value is |
escape, with no following digits, giving a character whose value is |
| 2989 |
zero. |
zero. |
| 2990 |
|
|
| 2991 |
Characters whose value is less than 256 can be defined by either of the |
Characters whose value is less than 256 can be defined by either of the |
| 2992 |
two syntaxes for \x. There is no difference in the way they are han- |
two syntaxes for \x. There is no difference in the way they are han- |
| 2993 |
dled. For example, \xdc is exactly the same as \x{dc}. |
dled. For example, \xdc is exactly the same as \x{dc}. |
| 2994 |
|
|
| 2995 |
After \0 up to two further octal digits are read. If there are fewer |
After \0 up to two further octal digits are read. If there are fewer |
| 2996 |
than two digits, just those that are present are used. Thus the |
than two digits, just those that are present are used. Thus the |
| 2997 |
sequence \0\x\07 specifies two binary zeros followed by a BEL character |
sequence \0\x\07 specifies two binary zeros followed by a BEL character |
| 2998 |
(code value 7). Make sure you supply two digits after the initial zero |
(code value 7). Make sure you supply two digits after the initial zero |
| 2999 |
if the pattern character that follows is itself an octal digit. |
if the pattern character that follows is itself an octal digit. |
| 3000 |
|
|
| 3001 |
The handling of a backslash followed by a digit other than 0 is compli- |
The handling of a backslash followed by a digit other than 0 is compli- |
| 3002 |
cated. Outside a character class, PCRE reads it and any following dig- |
cated. Outside a character class, PCRE reads it and any following dig- |
| 3003 |
its as a decimal number. If the number is less than 10, or if there |
its as a decimal number. If the number is less than 10, or if there |
| 3004 |
have been at least that many previous capturing left parentheses in the |
have been at least that many previous capturing left parentheses in the |
| 3005 |
expression, the entire sequence is taken as a back reference. A |
expression, the entire sequence is taken as a back reference. A |
| 3006 |
description of how this works is given later, following the discussion |
description of how this works is given later, following the discussion |
| 3007 |
of parenthesized subpatterns. |
of parenthesized subpatterns. |
| 3008 |
|
|
| 3009 |
Inside a character class, or if the decimal number is greater than 9 |
Inside a character class, or if the decimal number is greater than 9 |
| 3010 |
and there have not been that many capturing subpatterns, PCRE re-reads |
and there have not been that many capturing subpatterns, PCRE re-reads |
| 3011 |
up to three octal digits following the backslash, and uses them to gen- |
up to three octal digits following the backslash, and uses them to gen- |
| 3012 |
erate a data character. Any subsequent digits stand for themselves. In |
erate a data character. Any subsequent digits stand for themselves. In |
| 3013 |
non-UTF-8 mode, the value of a character specified in octal must be |
non-UTF-8 mode, the value of a character specified in octal must be |
| 3014 |
less than \400. In UTF-8 mode, values up to \777 are permitted. For |
less than \400. In UTF-8 mode, values up to \777 are permitted. For |
| 3015 |
example: |
example: |
| 3016 |
|
|
| 3017 |
\040 is another way of writing a space |
\040 is another way of writing a space |
| 3029 |
\81 is either a back reference, or a binary zero |
\81 is either a back reference, or a binary zero |
| 3030 |
followed by the two characters "8" and "1" |
followed by the two characters "8" and "1" |
| 3031 |
|
|
| 3032 |
Note that octal values of 100 or greater must not be introduced by a |
Note that octal values of 100 or greater must not be introduced by a |
| 3033 |
leading zero, because no more than three octal digits are ever read. |
leading zero, because no more than three octal digits are ever read. |
| 3034 |
|
|
| 3035 |
All the sequences that define a single character value can be used both |
All the sequences that define a single character value can be used both |
| 3036 |
inside and outside character classes. In addition, inside a character |
inside and outside character classes. In addition, inside a character |
| 3037 |
class, the sequence \b is interpreted as the backspace character (hex |
class, the sequence \b is interpreted as the backspace character (hex |
| 3038 |
08), and the sequences \R and \X are interpreted as the characters "R" |
08), and the sequences \R and \X are interpreted as the characters "R" |
| 3039 |
and "X", respectively. Outside a character class, these sequences have |
and "X", respectively. Outside a character class, these sequences have |
| 3040 |
different meanings (see below). |
different meanings (see below). |
| 3041 |
|
|
| 3042 |
Absolute and relative back references |
Absolute and relative back references |
| 3043 |
|
|
| 3044 |
The sequence \g followed by an unsigned or a negative number, option- |
The sequence \g followed by an unsigned or a negative number, option- |
| 3045 |
ally enclosed in braces, is an absolute or relative back reference. A |
ally enclosed in braces, is an absolute or relative back reference. A |
| 3046 |
named back reference can be coded as \g{name}. Back references are dis- |
named back reference can be coded as \g{name}. Back references are dis- |
| 3047 |
cussed later, following the discussion of parenthesized subpatterns. |
cussed later, following the discussion of parenthesized subpatterns. |
| 3048 |
|
|
| 3063 |
\W any "non-word" character |
\W any "non-word" character |
| 3064 |
|
|
| 3065 |
Each pair of escape sequences partitions the complete set of characters |
Each pair of escape sequences partitions the complete set of characters |
| 3066 |
into two disjoint sets. Any given character matches one, and only one, |
into two disjoint sets. Any given character matches one, and only one, |
| 3067 |
of each pair. |
of each pair. |
| 3068 |
|
|
| 3069 |
These character type sequences can appear both inside and outside char- |
These character type sequences can appear both inside and outside char- |
| 3070 |
acter classes. They each match one character of the appropriate type. |
acter classes. They each match one character of the appropriate type. |
| 3071 |
If the current matching point is at the end of the subject string, all |
If the current matching point is at the end of the subject string, all |
| 3072 |
of them fail, since there is no character to match. |
of them fail, since there is no character to match. |
| 3073 |
|
|
| 3074 |
For compatibility with Perl, \s does not match the VT character (code |
For compatibility with Perl, \s does not match the VT character (code |
| 3075 |
11). This makes it different from the the POSIX "space" class. The \s |
11). This makes it different from the the POSIX "space" class. The \s |
| 3076 |
characters are HT (9), LF (10), FF (12), CR (13), and space (32). If |
characters are HT (9), LF (10), FF (12), CR (13), and space (32). If |
| 3077 |
"use locale;" is included in a Perl script, \s may match the VT charac- |
"use locale;" is included in a Perl script, \s may match the VT charac- |
| 3078 |
ter. In PCRE, it never does. |
ter. In PCRE, it never does. |
| 3079 |
|
|
| 3080 |
In UTF-8 mode, characters with values greater than 128 never match \d, |
In UTF-8 mode, characters with values greater than 128 never match \d, |
| 3081 |
\s, or \w, and always match \D, \S, and \W. This is true even when Uni- |
\s, or \w, and always match \D, \S, and \W. This is true even when Uni- |
| 3082 |
code character property support is available. These sequences retain |
code character property support is available. These sequences retain |
| 3083 |
their original meanings from before UTF-8 support was available, mainly |
their original meanings from before UTF-8 support was available, mainly |
| 3084 |
for efficiency reasons. |
for efficiency reasons. |
| 3085 |
|
|
| 3086 |
The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to |
The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to |
| 3087 |
the other sequences, these do match certain high-valued codepoints in |
the other sequences, these do match certain high-valued codepoints in |
| 3088 |
UTF-8 mode. The horizontal space characters are: |
UTF-8 mode. The horizontal space characters are: |
| 3089 |
|
|
| 3090 |
U+0009 Horizontal tab |
U+0009 Horizontal tab |
| 3118 |
U+2029 Paragraph separator |
U+2029 Paragraph separator |
| 3119 |
|
|
| 3120 |
A "word" character is an underscore or any character less than 256 that |
A "word" character is an underscore or any character less than 256 that |
| 3121 |
is a letter or digit. The definition of letters and digits is con- |
is a letter or digit. The definition of letters and digits is con- |
| 3122 |
trolled by PCRE's low-valued character tables, and may vary if locale- |
trolled by PCRE's low-valued character tables, and may vary if locale- |
| 3123 |
specific matching is taking place (see "Locale support" in the pcreapi |
specific matching is taking place (see "Locale support" in the pcreapi |
| 3124 |
page). For example, in a French locale such as "fr_FR" in Unix-like |
page). For example, in a French locale such as "fr_FR" in Unix-like |
| 3125 |
systems, or "french" in Windows, some character codes greater than 128 |
systems, or "french" in Windows, some character codes greater than 128 |
| 3126 |
are used for accented letters, and these are matched by \w. The use of |
are used for accented letters, and these are matched by \w. The use of |
| 3127 |
locales with Unicode is discouraged. |
locales with Unicode is discouraged. |
| 3128 |
|
|
| 3129 |
Newline sequences |
Newline sequences |
| 3130 |
|
|
| 3131 |
Outside a character class, the escape sequence \R matches any Unicode |
Outside a character class, the escape sequence \R matches any Unicode |
| 3132 |
newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is |
newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is |
| 3133 |
equivalent to the following: |
equivalent to the following: |
| 3134 |
|
|
| 3135 |
(?>\r\n|\n|\x0b|\f|\r|\x85) |
(?>\r\n|\n|\x0b|\f|\r|\x85) |
| 3136 |
|
|
| 3137 |
This is an example of an "atomic group", details of which are given |
This is an example of an "atomic group", details of which are given |
| 3138 |
below. This particular group matches either the two-character sequence |
below. This particular group matches either the two-character sequence |
| 3139 |
CR followed by LF, or one of the single characters LF (linefeed, |
CR followed by LF, or one of the single characters LF (linefeed, |
| 3140 |
U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage |
U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage |
| 3141 |
return, U+000D), or NEL (next line, U+0085). The two-character sequence |
return, U+000D), or NEL (next line, U+0085). The two-character sequence |
| 3142 |
is treated as a single unit that cannot be split. |
is treated as a single unit that cannot be split. |
| 3143 |
|
|
| 3144 |
In UTF-8 mode, two additional characters whose codepoints are greater |
In UTF-8 mode, two additional characters whose codepoints are greater |
| 3145 |
than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa- |
than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa- |
| 3146 |
rator, U+2029). Unicode character property support is not needed for |
rator, U+2029). Unicode character property support is not needed for |
| 3147 |
these characters to be recognized. |
these characters to be recognized. |
| 3148 |
|
|
| 3149 |
Inside a character class, \R matches the letter "R". |
Inside a character class, \R matches the letter "R". |
| 3151 |
Unicode character properties |
Unicode character properties |
| 3152 |
|
|
| 3153 |
When PCRE is built with Unicode character property support, three addi- |
When PCRE is built with Unicode character property support, three addi- |
| 3154 |
tional escape sequences that match characters with specific properties |
tional escape sequences that match characters with specific properties |
| 3155 |
are available. When not in UTF-8 mode, these sequences are of course |
are available. When not in UTF-8 mode, these sequences are of course |
| 3156 |
limited to testing characters whose codepoints are less than 256, but |
limited to testing characters whose codepoints are less than 256, but |
| 3157 |
they do work in this mode. The extra escape sequences are: |
they do work in this mode. The extra escape sequences are: |
| 3158 |
|
|
| 3159 |
\p{xx} a character with the xx property |
\p{xx} a character with the xx property |
| 3160 |
\P{xx} a character without the xx property |
\P{xx} a character without the xx property |
| 3161 |
\X an extended Unicode sequence |
\X an extended Unicode sequence |
| 3162 |
|
|
| 3163 |
The property names represented by xx above are limited to the Unicode |
The property names represented by xx above are limited to the Unicode |
| 3164 |
script names, the general category properties, and "Any", which matches |
script names, the general category properties, and "Any", which matches |
| 3165 |
any character (including newline). Other properties such as "InMusical- |
any character (including newline). Other properties such as "InMusical- |
| 3166 |
Symbols" are not currently supported by PCRE. Note that \P{Any} does |
Symbols" are not currently supported by PCRE. Note that \P{Any} does |
| 3167 |
not match any characters, so always causes a match failure. |
not match any characters, so always causes a match failure. |
| 3168 |
|
|
| 3169 |
Sets of Unicode characters are defined as belonging to certain scripts. |
Sets of Unicode characters are defined as belonging to certain scripts. |
| 3170 |
A character from one of these sets can be matched using a script name. |
A character from one of these sets can be matched using a script name. |
| 3171 |
For example: |
For example: |
| 3172 |
|
|
| 3173 |
\p{Greek} |
\p{Greek} |
| 3174 |
\P{Han} |
\P{Han} |
| 3175 |
|
|
| 3176 |
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 |
| 3177 |
"Common". The current list of scripts is: |
"Common". The current list of scripts is: |
| 3178 |
|
|
| 3179 |
Arabic, Armenian, Balinese, Bengali, Bopomofo, Braille, Buginese, |
Arabic, Armenian, Balinese, Bengali, Bopomofo, Braille, Buginese, |
| 3180 |
Buhid, Canadian_Aboriginal, Cherokee, Common, Coptic, Cuneiform, |
Buhid, Canadian_Aboriginal, Cherokee, Common, Coptic, Cuneiform, |
| 3181 |
Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic, |
Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic, |
| 3182 |
Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira- |
Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira- |
| 3183 |
gana, Inherited, Kannada, Katakana, Kharoshthi, Khmer, Lao, Latin, |
gana, Inherited, Kannada, Katakana, Kharoshthi, Khmer, Lao, Latin, |
| 3184 |
Limbu, Linear_B, Malayalam, Mongolian, Myanmar, New_Tai_Lue, Nko, |
Limbu, Linear_B, Malayalam, Mongolian, Myanmar, New_Tai_Lue, Nko, |
| 3185 |
Ogham, Old_Italic, Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician, |
Ogham, Old_Italic, Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician, |
| 3186 |
Runic, Shavian, Sinhala, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, |
Runic, Shavian, Sinhala, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, |
| 3187 |
Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi. |
Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi. |
| 3188 |
|
|
| 3189 |
Each character has exactly one general category property, specified by |
Each character has exactly one general category property, specified by |
| 3190 |
a two-letter abbreviation. For compatibility with Perl, negation can be |
a two-letter abbreviation. For compatibility with Perl, negation can be |
| 3191 |
specified by including a circumflex between the opening brace and the |
specified by including a circumflex between the opening brace and the |
| 3192 |
property name. For example, \p{^Lu} is the same as \P{Lu}. |
property name. For example, \p{^Lu} is the same as \P{Lu}. |
| 3193 |
|
|
| 3194 |
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- |
| 3195 |
eral category properties that start with that letter. In this case, in |
eral category properties that start with that letter. In this case, in |
| 3196 |
the absence of negation, the curly brackets in the escape sequence are |
the absence of negation, the curly brackets in the escape sequence are |
| 3197 |
optional; these two examples have the same effect: |
optional; these two examples have the same effect: |
| 3198 |
|
|
| 3199 |
\p{L} |
\p{L} |
| 3245 |
Zp Paragraph separator |
Zp Paragraph separator |
| 3246 |
Zs Space separator |
Zs Space separator |
| 3247 |
|
|
| 3248 |
The special property L& is also supported: it matches a character that |
The special property L& is also supported: it matches a character that |
| 3249 |
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 |
| 3250 |
classified as a modifier or "other". |
classified as a modifier or "other". |
| 3251 |
|
|
| 3252 |
The Cs (Surrogate) property applies only to characters in the range |
The Cs (Surrogate) property applies only to characters in the range |
| 3253 |
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 |
| 3254 |
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- |
| 3255 |
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 |
| 3256 |
the pcreapi page). |
the pcreapi page). |
| 3257 |
|
|
| 3258 |
The long synonyms for these properties that Perl supports (such as |
The long synonyms for these properties that Perl supports (such as |
| 3259 |
\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 |
| 3260 |
any of these properties with "Is". |
any of these properties with "Is". |
| 3261 |
|
|
| 3262 |
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- |
| 3263 |
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 |
| 3264 |
in the Unicode table. |
in the Unicode table. |
| 3265 |
|
|
| 3266 |
Specifying caseless matching does not affect these escape sequences. |
Specifying caseless matching does not affect these escape sequences. |
| 3267 |
For example, \p{Lu} always matches only upper case letters. |
For example, \p{Lu} always matches only upper case letters. |
| 3268 |
|
|
| 3269 |
The \X escape matches any number of Unicode characters that form an |
The \X escape matches any number of Unicode characters that form an |
| 3270 |
extended Unicode sequence. \X is equivalent to |
extended Unicode sequence. \X is equivalent to |
| 3271 |
|
|
| 3272 |
(?>\PM\pM*) |
(?>\PM\pM*) |
| 3273 |
|
|
| 3274 |
That is, it matches a character without the "mark" property, followed |
That is, it matches a character without the "mark" property, followed |
| 3275 |
by zero or more characters with the "mark" property, and treats the |
by zero or more characters with the "mark" property, and treats the |
| 3276 |
sequence as an atomic group (see below). Characters with the "mark" |
sequence as an atomic group (see below). Characters with the "mark" |
| 3277 |
property are typically accents that affect the preceding character. |
property are typically accents that affect the preceding character. |
| 3278 |
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 |
| 3279 |
matches any one character. |
matches any one character. |
| 3280 |
|
|
| 3281 |
Matching characters by Unicode property is not fast, because PCRE has |
Matching characters by Unicode property is not fast, because PCRE has |
| 3282 |
to search a structure that contains data for over fifteen thousand |
to search a structure that contains data for over fifteen thousand |
| 3283 |
characters. That is why the traditional escape sequences such as \d and |
characters. That is why the traditional escape sequences such as \d and |
| 3284 |
\w do not use Unicode properties in PCRE. |
\w do not use Unicode properties in PCRE. |
| 3285 |
|
|
| 3286 |
Resetting the match start |
Resetting the match start |
| 3287 |
|
|
| 3288 |
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- |
| 3289 |
ously matched characters not to be included in the final matched |
ously matched characters not to be included in the final matched |
| 3290 |
sequence. For example, the pattern: |
sequence. For example, the pattern: |
| 3291 |
|
|
| 3292 |
foo\Kbar |
foo\Kbar |
| 3293 |
|
|
| 3294 |
matches "foobar", but reports that it has matched "bar". This feature |
matches "foobar", but reports that it has matched "bar". This feature |
| 3295 |
is similar to a lookbehind assertion (described below). However, in |
is similar to a lookbehind assertion (described below). However, in |
| 3296 |
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 |
| 3297 |
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 |
| 3298 |
not interfere with the setting of captured substrings. For example, |
not interfere with the setting of captured substrings. For example, |
| 3299 |
when the pattern |
when the pattern |
| 3300 |
|
|
| 3301 |
(foo)\Kbar |
(foo)\Kbar |
| 3304 |
|
|
| 3305 |
Simple assertions |
Simple assertions |
| 3306 |
|
|
| 3307 |
The final use of backslash is for certain simple assertions. An asser- |
The final use of backslash is for certain simple assertions. An asser- |
| 3308 |
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 |
| 3309 |
a match, without consuming any characters from the subject string. The |
a match, without consuming any characters from the subject string. The |
| 3310 |
use of subpatterns for more complicated assertions is described below. |
use of subpatterns for more complicated assertions is described below. |
| 3311 |
The backslashed assertions are: |
The backslashed assertions are: |
| 3312 |
|
|
| 3313 |
\b matches at a word boundary |
\b matches at a word boundary |
| 3318 |
\z matches only at the end of the subject |
\z matches only at the end of the subject |
| 3319 |
\G matches at the first matching position in the subject |
\G matches at the first matching position in the subject |
| 3320 |
|
|
| 3321 |
These assertions may not appear in character classes (but note that \b |
These assertions may not appear in character classes (but note that \b |
| 3322 |
has a different meaning, namely the backspace character, inside a char- |
has a different meaning, namely the backspace character, inside a char- |
| 3323 |
acter class). |
acter class). |
| 3324 |
|
|
| 3325 |
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 |
| 3326 |
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. |
| 3327 |
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 |
| 3328 |
string if the first or last character matches \w, respectively. |
string if the first or last character matches \w, respectively. |
| 3329 |
|
|
| 3330 |
The \A, \Z, and \z assertions differ from the traditional circumflex |
The \A, \Z, and \z assertions differ from the traditional circumflex |
| 3331 |
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 |
| 3332 |
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 |
| 3333 |
set. Thus, they are independent of multiline mode. These three asser- |
set. Thus, they are independent of multiline mode. These three asser- |
| 3334 |
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 |
| 3335 |
affect only the behaviour of the circumflex and dollar metacharacters. |
affect only the behaviour of the circumflex and dollar metacharacters. |
| 3336 |
However, if the startoffset argument of pcre_exec() is non-zero, indi- |
However, if the startoffset argument of pcre_exec() is non-zero, indi- |
| 3337 |
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 |
| 3338 |
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 |
| 3339 |
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 |
| 3340 |
the very end, whereas \z matches only at the end. |
the very end, whereas \z matches only at the end. |
| 3341 |
|
|
| 3342 |
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 |
| 3343 |
the start point of the match, as specified by the startoffset argument |
the start point of the match, as specified by the startoffset argument |
| 3344 |
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 |
| 3345 |
non-zero. By calling pcre_exec() multiple times with appropriate argu- |
non-zero. By calling pcre_exec() multiple times with appropriate argu- |
| 3346 |
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- |
| 3347 |
mentation where \G can be useful. |
mentation where \G can be useful. |
| 3348 |
|
|
| 3349 |
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 |
| 3350 |
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 |
| 3351 |
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 |
| 3352 |
previously matched string was empty. Because PCRE does just one match |
previously matched string was empty. Because PCRE does just one match |
| 3353 |
at a time, it cannot reproduce this behaviour. |
at a time, it cannot reproduce this behaviour. |
| 3354 |
|
|
| 3355 |
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 |
| 3356 |
anchored to the starting match position, and the "anchored" flag is set |
anchored to the starting match position, and the "anchored" flag is set |
| 3357 |
in the compiled regular expression. |
in the compiled regular expression. |
| 3358 |
|
|
| 3360 |
CIRCUMFLEX AND DOLLAR |
CIRCUMFLEX AND DOLLAR |
| 3361 |
|
|
| 3362 |
Outside a character class, in the default matching mode, the circumflex |
Outside a character class, in the default matching mode, the circumflex |
| 3363 |
character is an assertion that is true only if the current matching |
character is an assertion that is true only if the current matching |
| 3364 |
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- |
| 3365 |
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 |
| 3366 |
PCRE_MULTILINE option is unset. Inside a character class, circumflex |
PCRE_MULTILINE option is unset. Inside a character class, circumflex |
| 3367 |
has an entirely different meaning (see below). |
has an entirely different meaning (see below). |
| 3368 |
|
|
| 3369 |
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 |
| 3370 |
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 |
| 3371 |
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 |
| 3372 |
branch. If all possible alternatives start with a circumflex, that is, |
branch. If all possible alternatives start with a circumflex, that is, |
| 3373 |
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- |
| 3374 |
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 |
| 3375 |
constructs that can cause a pattern to be anchored.) |
constructs that can cause a pattern to be anchored.) |
| 3376 |
|
|
| 3377 |
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 |
| 3378 |
matching point is at the end of the subject string, or immediately |
matching point is at the end of the subject string, or immediately |
| 3379 |
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 |
| 3380 |
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 |
| 3381 |
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 |
| 3382 |
appears. Dollar has no special meaning in a character class. |
appears. Dollar has no special meaning in a character class. |
| 3383 |
|
|
| 3384 |
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 |
| 3385 |
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 |
| 3386 |
compile time. This does not affect the \Z assertion. |
compile time. This does not affect the \Z assertion. |
| 3387 |
|
|
| 3388 |
The meanings of the circumflex and dollar characters are changed if the |
The meanings of the circumflex and dollar characters are changed if the |
| 3389 |
PCRE_MULTILINE option is set. When this is the case, a circumflex |
PCRE_MULTILINE option is set. When this is the case, a circumflex |
| 3390 |
matches immediately after internal newlines as well as at the start of |
matches immediately after internal newlines as well as at the start of |
| 3391 |
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 |
| 3392 |
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 |
| 3393 |
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 |
| 3394 |
as the two-character sequence CRLF, isolated CR and LF characters do |
as the two-character sequence CRLF, isolated CR and LF characters do |
| 3395 |
not indicate newlines. |
not indicate newlines. |
| 3396 |
|
|
| 3397 |
For example, the pattern /^abc$/ matches the subject string "def\nabc" |
For example, the pattern /^abc$/ matches the subject string "def\nabc" |
| 3398 |
(where \n represents a newline) in multiline mode, but not otherwise. |
(where \n represents a newline) in multiline mode, but not otherwise. |
| 3399 |
Consequently, patterns that are anchored in single line mode because |
Consequently, patterns that are anchored in single line mode because |
| 3400 |
all branches start with ^ are not anchored in multiline mode, and a |
all branches start with ^ are not anchored in multiline mode, and a |
| 3401 |
match for circumflex is possible when the startoffset argument of |
match for circumflex is possible when the startoffset argument of |
| 3402 |
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 |
| 3403 |
PCRE_MULTILINE is set. |
PCRE_MULTILINE is set. |
| 3404 |
|
|
| 3405 |
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 |
| 3406 |
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 |
| 3407 |
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 |
| 3408 |
set. |
set. |
| 3409 |
|
|
| 3410 |
|
|
| 3411 |
FULL STOP (PERIOD, DOT) |
FULL STOP (PERIOD, DOT) |
| 3412 |
|
|
| 3413 |
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- |
| 3414 |
ter in the subject string except (by default) a character that signi- |
ter in the subject string except (by default) a character that signi- |
| 3415 |
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 |
| 3416 |
more than one byte long. |
more than one byte long. |
| 3417 |
|
|
| 3418 |
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 |
| 3419 |
that character; when the two-character sequence CRLF is used, dot does |
that character; when the two-character sequence CRLF is used, dot does |
| 3420 |
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 |
| 3421 |
matches all characters (including isolated CRs and LFs). When any Uni- |
matches all characters (including isolated CRs and LFs). When any Uni- |
| 3422 |
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 |
| 3423 |
any of the other line ending characters. |
any of the other line ending characters. |
| 3424 |
|
|
| 3425 |
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 |
| 3426 |
PCRE_DOTALL option is set, a dot matches any one character, without |
PCRE_DOTALL option is set, a dot matches any one character, without |
| 3427 |
exception. If the two-character sequence CRLF is present in the subject |
exception. If the two-character sequence CRLF is present in the subject |
| 3428 |
string, it takes two dots to match it. |
string, it takes two dots to match it. |
| 3429 |
|
|
| 3430 |
The handling of dot is entirely independent of the handling of circum- |
The handling of dot is entirely independent of the handling of circum- |
| 3431 |
flex and dollar, the only relationship being that they both involve |
flex and dollar, the only relationship being that they both involve |
| 3432 |
newlines. Dot has no special meaning in a character class. |
newlines. Dot has no special meaning in a character class. |
| 3433 |
|
|
| 3434 |
|
|
| 3435 |
MATCHING A SINGLE BYTE |
MATCHING A SINGLE BYTE |
| 3436 |
|
|
| 3437 |
Outside a character class, the escape sequence \C matches any one byte, |
Outside a character class, the escape sequence \C matches any one byte, |
| 3438 |
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 |
| 3439 |
line-ending characters. The feature is provided in Perl in order to |
line-ending characters. The feature is provided in Perl in order to |
| 3440 |
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- |
| 3441 |
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- |
| 3442 |
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 |
| 3443 |
avoided. |
avoided. |
| 3444 |
|
|
| 3445 |
PCRE does not allow \C to appear in lookbehind assertions (described |
PCRE does not allow \C to appear in lookbehind assertions (described |
| 3446 |
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- |
| 3447 |
late the length of the lookbehind. |
late the length of the lookbehind. |
| 3448 |
|
|
| 3449 |
|
|
| 3452 |
An opening square bracket introduces a character class, terminated by a |
An opening square bracket introduces a character class, terminated by a |
| 3453 |
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- |
| 3454 |
cial. If a closing square bracket is required as a member of the class, |
cial. If a closing square bracket is required as a member of the class, |
| 3455 |
it should be the first data character in the class (after an initial |
it should be the first data character in the class (after an initial |
| 3456 |
circumflex, if present) or escaped with a backslash. |
circumflex, if present) or escaped with a backslash. |
| 3457 |
|
|
| 3458 |
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 |
| 3459 |
mode, the character may occupy more than one byte. A matched character |
mode, the character may occupy more than one byte. A matched character |
| 3460 |
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 |
| 3461 |
character in the class definition is a circumflex, in which case the |
character in the class definition is a circumflex, in which case the |
| 3462 |
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 |
| 3463 |
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 |
| 3464 |
not the first character, or escape it with a backslash. |
not the first character, or escape it with a backslash. |
| 3465 |
|
|
| 3466 |
For example, the character class [aeiou] matches any lower case vowel, |
For example, the character class [aeiou] matches any lower case vowel, |
| 3467 |
while [^aeiou] matches any character that is not a lower case vowel. |
while [^aeiou] matches any character that is not a lower case vowel. |
| 3468 |
Note that a circumflex is just a convenient notation for specifying the |
Note that a circumflex is just a convenient notation for specifying the |
| 3469 |
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 |
| 3470 |
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- |
| 3471 |
sumes a character from the subject string, and therefore it fails if |
sumes a character from the subject string, and therefore it fails if |
| 3472 |
the current pointer is at the end of the string. |
the current pointer is at the end of the string. |
| 3473 |
|
|
| 3474 |
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 |
| 3475 |
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 |
| 3476 |
mechanism. |
mechanism. |
| 3477 |
|
|
| 3478 |
When caseless matching is set, any letters in a class represent both |
When caseless matching is set, any letters in a class represent both |
| 3479 |
their upper case and lower case versions, so for example, a caseless |
their upper case and lower case versions, so for example, a caseless |
| 3480 |
[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 |
| 3481 |
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 |
| 3482 |
understands the concept of case for characters whose values are less |
understands the concept of case for characters whose values are less |
| 3483 |
than 128, so caseless matching is always possible. For characters with |
than 128, so caseless matching is always possible. For characters with |
| 3484 |
higher values, the concept of case is supported if PCRE is compiled |
higher values, the concept of case is supported if PCRE is compiled |
| 3485 |
with Unicode property support, but not otherwise. If you want to use |
with Unicode property support, but not otherwise. If you want to use |
| 3486 |
caseless matching for characters 128 and above, you must ensure that |
caseless matching for characters 128 and above, you must ensure that |
| 3487 |
PCRE is compiled with Unicode property support as well as with UTF-8 |
PCRE is compiled with Unicode property support as well as with UTF-8 |
| 3488 |
support. |
support. |
| 3489 |
|
|
| 3490 |
Characters that might indicate line breaks are never treated in any |
Characters that might indicate line breaks are never treated in any |
| 3491 |
special way when matching character classes, whatever line-ending |
special way when matching character classes, whatever line-ending |
| 3492 |
sequence is in use, and whatever setting of the PCRE_DOTALL and |
sequence is in use, and whatever setting of the PCRE_DOTALL and |
| 3493 |
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 |
| 3494 |
of these characters. |
of these characters. |
| 3495 |
|
|
| 3496 |
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- |
| 3497 |
ters in a character class. For example, [d-m] matches any letter |
ters in a character class. For example, [d-m] matches any letter |
| 3498 |
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 |
| 3499 |
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 |
| 3500 |
where it cannot be interpreted as indicating a range, typically as the |
where it cannot be interpreted as indicating a range, typically as the |
| 3501 |
first or last character in the class. |
first or last character in the class. |
| 3502 |
|
|
| 3503 |
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- |
| 3504 |
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 |
| 3505 |
two characters ("W" and "-") followed by a literal string "46]", so it |
two characters ("W" and "-") followed by a literal string "46]", so it |
| 3506 |
would match "W46]" or "-46]". However, if the "]" is escaped with a |
would match "W46]" or "-46]". However, if the "]" is escaped with a |
| 3507 |
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- |
| 3508 |
preted as a class containing a range followed by two other characters. |
preted as a class containing a range followed by two other characters. |
| 3509 |
The octal or hexadecimal representation of "]" can also be used to end |
The octal or hexadecimal representation of "]" can also be used to end |
| 3510 |
a range. |
a range. |
| 3511 |
|
|
| 3512 |
Ranges operate in the collating sequence of character values. They can |
Ranges operate in the collating sequence of character values. They can |
| 3513 |
also be used for characters specified numerically, for example |
also be used for characters specified numerically, for example |
| 3514 |
[\000-\037]. In UTF-8 mode, ranges can include characters whose values |
[\000-\037]. In UTF-8 mode, ranges can include characters whose values |
| 3515 |
are greater than 255, for example [\x{100}-\x{2ff}]. |
are greater than 255, for example [\x{100}-\x{2ff}]. |
| 3516 |
|
|
| 3517 |
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, |
| 3518 |
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 |
| 3519 |
to [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if |
to [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if |
| 3520 |
character tables for a French locale are in use, [\xc8-\xcb] matches |
character tables for a French locale are in use, [\xc8-\xcb] matches |
| 3521 |
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 |
| 3522 |
concept of case for characters with values greater than 128 only when |
concept of case for characters with values greater than 128 only when |
| 3523 |
it is compiled with Unicode property support. |
it is compiled with Unicode property support. |
| 3524 |
|
|
| 3525 |
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 |
| 3526 |
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 |
| 3527 |
class. For example, [\dABCDEF] matches any hexadecimal digit. A circum- |
class. For example, [\dABCDEF] matches any hexadecimal digit. A circum- |
| 3528 |
flex can conveniently be used with the upper case character types to |
flex can conveniently be used with the upper case character types to |
| 3529 |
specify a more restricted set of characters than the matching lower |
specify a more restricted set of characters than the matching lower |
| 3530 |
case type. For example, the class [^\W_] matches any letter or digit, |
case type. For example, the class [^\W_] matches any letter or digit, |
| 3531 |
but not underscore. |
but not underscore. |
| 3532 |
|
|
| 3533 |
The only metacharacters that are recognized in character classes are |
The only metacharacters that are recognized in character classes are |
| 3534 |
backslash, hyphen (only where it can be interpreted as specifying a |
backslash, hyphen (only where it can be interpreted as specifying a |
| 3535 |
range), circumflex (only at the start), opening square bracket (only |
range), circumflex (only at the start), opening square bracket (only |
| 3536 |
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 |
| 3537 |
next section), and the terminating closing square bracket. However, |
next section), and the terminating closing square bracket. However, |
| 3538 |
escaping other non-alphanumeric characters does no harm. |
escaping other non-alphanumeric characters does no harm. |
| 3539 |
|
|
| 3540 |
|
|
| 3541 |
POSIX CHARACTER CLASSES |
POSIX CHARACTER CLASSES |
| 3542 |
|
|
| 3543 |
Perl supports the POSIX notation for character classes. This uses names |
Perl supports the POSIX notation for character classes. This uses names |
| 3544 |
enclosed by [: and :] within the enclosing square brackets. PCRE also |
enclosed by [: and :] within the enclosing square brackets. PCRE also |
| 3545 |
supports this notation. For example, |
supports this notation. For example, |
| 3546 |
|
|
| 3547 |
[01[:alpha:]%] |
[01[:alpha:]%] |
| 3564 |
word "word" characters (same as \w) |
word "word" characters (same as \w) |
| 3565 |
xdigit hexadecimal digits |
xdigit hexadecimal digits |
| 3566 |
|
|
| 3567 |
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), |
| 3568 |
and space (32). Notice that this list includes the VT character (code |
and space (32). Notice that this list includes the VT character (code |
| 3569 |
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 |
| 3570 |
Perl compatibility). |
Perl compatibility). |
| 3571 |
|
|
| 3572 |
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 |
| 3573 |
from Perl 5.8. Another Perl extension is negation, which is indicated |
from Perl 5.8. Another Perl extension is negation, which is indicated |
| 3574 |
by a ^ character after the colon. For example, |
by a ^ character after the colon. For example, |
| 3575 |
|
|
| 3576 |
[12[:^digit:]] |
[12[:^digit:]] |
| 3577 |
|
|
| 3578 |
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 |
| 3579 |
POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but |
POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but |
| 3580 |
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. |
| 3581 |
|
|
| 3585 |
|
|
| 3586 |
VERTICAL BAR |
VERTICAL BAR |
| 3587 |
|
|
| 3588 |
Vertical bar characters are used to separate alternative patterns. For |
Vertical bar characters are used to separate alternative patterns. For |
| 3589 |
example, the pattern |
example, the pattern |
| 3590 |
|
|
| 3591 |
gilbert|sullivan |
gilbert|sullivan |
| 3592 |
|
|
| 3593 |
matches either "gilbert" or "sullivan". Any number of alternatives may |
matches either "gilbert" or "sullivan". Any number of alternatives may |
| 3594 |
appear, and an empty alternative is permitted (matching the empty |
appear, and an empty alternative is permitted (matching the empty |
| 3595 |
string). The matching process tries each alternative in turn, from left |
string). The matching process tries each alternative in turn, from left |
| 3596 |
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 |
| 3597 |
are within a subpattern (defined below), "succeeds" means matching the |
are within a subpattern (defined below), "succeeds" means matching the |
| 3598 |
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. |
| 3599 |
|
|
| 3600 |
|
|
| 3601 |
INTERNAL OPTION SETTING |
INTERNAL OPTION SETTING |
| 3602 |
|
|
| 3603 |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
| 3604 |
PCRE_EXTENDED options can be changed from within the pattern by a |
PCRE_EXTENDED options can be changed from within the pattern by a |
| 3605 |
sequence of Perl option letters enclosed between "(?" and ")". The |
sequence of Perl option letters enclosed between "(?" and ")". The |
| 3606 |
option letters are |
option letters are |
| 3607 |
|
|
| 3608 |
i for PCRE_CASELESS |
i for PCRE_CASELESS |
| 3612 |
|
|
| 3613 |
For example, (?im) sets caseless, multiline matching. It is also possi- |
For example, (?im) sets caseless, multiline matching. It is also possi- |
| 3614 |
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 |
| 3615 |
combined setting and unsetting such as (?im-sx), which sets PCRE_CASE- |
combined setting and unsetting such as (?im-sx), which sets PCRE_CASE- |
| 3616 |
LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, |
LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, |
| 3617 |
is also permitted. If a letter appears both before and after the |
is also permitted. If a letter appears both before and after the |
| 3618 |
hyphen, the option is unset. |
hyphen, the option is unset. |
| 3619 |
|
|
| 3620 |
When an option change occurs at top level (that is, not inside subpat- |
When an option change occurs at top level (that is, not inside subpat- |
| 3621 |
tern parentheses), the change applies to the remainder of the pattern |
tern parentheses), the change applies to the remainder of the pattern |
| 3622 |
that follows. If the change is placed right at the start of a pattern, |
that follows. If the change is placed right at the start of a pattern, |
| 3623 |
PCRE extracts it into the global options (and it will therefore show up |
PCRE extracts it into the global options (and it will therefore show up |
| 3624 |
in data extracted by the pcre_fullinfo() function). |
in data extracted by the pcre_fullinfo() function). |
| 3625 |
|
|
| 3626 |
An option change within a subpattern (see below for a description of |
An option change within a subpattern (see below for a description of |
| 3627 |
subpatterns) affects only that part of the current pattern that follows |
subpatterns) affects only that part of the current pattern that follows |
| 3628 |
it, so |
it, so |
| 3629 |
|
|
| 3630 |
(a(?i)b)c |
(a(?i)b)c |
| 3631 |
|
|
| 3632 |
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 |
| 3633 |
used). By this means, options can be made to have different settings |
used). By this means, options can be made to have different settings |
| 3634 |
in different parts of the pattern. Any changes made in one alternative |
in different parts of the pattern. Any changes made in one alternative |
| 3635 |
do carry on into subsequent branches within the same subpattern. For |
do carry on into subsequent branches within the same subpattern. For |
| 3636 |
example, |
example, |
| 3637 |
|
|
| 3638 |
(a(?i)b|c) |
(a(?i)b|c) |
| 3639 |
|
|
| 3640 |
matches "ab", "aB", "c", and "C", even though when matching "C" the |
matches "ab", "aB", "c", and "C", even though when matching "C" the |
| 3641 |
first branch is abandoned before the option setting. This is because |
first branch is abandoned before the option setting. This is because |
| 3642 |
the effects of option settings happen at compile time. There would be |
the effects of option settings happen at compile time. There would be |
| 3643 |
some very weird behaviour otherwise. |
some very weird behaviour otherwise. |
| 3644 |
|
|
| 3645 |
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA |
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA |
| 3646 |
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 |
| 3647 |
the characters J, U and X respectively. |
the characters J, U and X respectively. |
| 3648 |
|
|
| 3649 |
|
|
| 3656 |
|
|
| 3657 |
cat(aract|erpillar|) |
cat(aract|erpillar|) |
| 3658 |
|
|
| 3659 |
matches one of the words "cat", "cataract", or "caterpillar". Without |
matches one of the words "cat", "cataract", or "caterpillar". Without |
| 3660 |
the parentheses, it would match "cataract", "erpillar" or an empty |
the parentheses, it would match "cataract", "erpillar" or an empty |
| 3661 |
string. |
string. |
| 3662 |
|
|
| 3663 |
2. It sets up the subpattern as a capturing subpattern. This means |
2. It sets up the subpattern as a capturing subpattern. This means |
| 3664 |
that, when the whole pattern matches, that portion of the subject |
that, when the whole pattern matches, that portion of the subject |
| 3665 |
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 |
| 3666 |
ovector argument of pcre_exec(). Opening parentheses are counted from |
ovector argument of pcre_exec(). Opening parentheses are counted from |
| 3667 |
left to right (starting from 1) to obtain numbers for the capturing |
left to right (starting from 1) to obtain numbers for the capturing |
| 3668 |
subpatterns. |
subpatterns. |
| 3669 |
|
|
| 3670 |
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- |
| 3671 |
tern |
tern |
| 3672 |
|
|
| 3673 |
the ((red|white) (king|queen)) |
the ((red|white) (king|queen)) |
| 3675 |
the captured substrings are "red king", "red", and "king", and are num- |
the captured substrings are "red king", "red", and "king", and are num- |
| 3676 |
bered 1, 2, and 3, respectively. |
bered 1, 2, and 3, respectively. |
| 3677 |
|
|
| 3678 |
The fact that plain parentheses fulfil two functions is not always |
The fact that plain parentheses fulfil two functions is not always |
| 3679 |
helpful. There are often times when a grouping subpattern is required |
helpful. There are often times when a grouping subpattern is required |
| 3680 |
without a capturing requirement. If an opening parenthesis is followed |
without a capturing requirement. If an opening parenthesis is followed |
| 3681 |
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- |
| 3682 |
ing, and is not counted when computing the number of any subsequent |
ing, and is not counted when computing the number of any subsequent |
| 3683 |
capturing subpatterns. For example, if the string "the white queen" is |
capturing subpatterns. For example, if the string "the white queen" is |
| 3684 |
matched against the pattern |
matched against the pattern |
| 3685 |
|
|
| 3686 |
the ((?:red|white) (king|queen)) |
the ((?:red|white) (king|queen)) |
| 3688 |
the captured substrings are "white queen" and "queen", and are numbered |
the captured substrings are "white queen" and "queen", and are numbered |
| 3689 |
1 and 2. The maximum number of capturing subpatterns is 65535. |
1 and 2. The maximum number of capturing subpatterns is 65535. |
| 3690 |
|
|
| 3691 |
As a convenient shorthand, if any option settings are required at the |
As a convenient shorthand, if any option settings are required at the |
| 3692 |
start of a non-capturing subpattern, the option letters may appear |
start of a non-capturing subpattern, the option letters may appear |
| 3693 |
between the "?" and the ":". Thus the two patterns |
between the "?" and the ":". Thus the two patterns |
| 3694 |
|
|
| 3695 |
(?i:saturday|sunday) |
(?i:saturday|sunday) |
| 3696 |
(?:(?i)saturday|sunday) |
(?:(?i)saturday|sunday) |
| 3697 |
|
|
| 3698 |
match exactly the same set of strings. Because alternative branches are |
match exactly the same set of strings. Because alternative branches are |
| 3699 |
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 |
| 3700 |
the subpattern is reached, an option setting in one branch does affect |
the subpattern is reached, an option setting in one branch does affect |
| 3701 |
subsequent branches, so the above patterns match "SUNDAY" as well as |
subsequent branches, so the above patterns match "SUNDAY" as well as |
| 3702 |
"Saturday". |
"Saturday". |
| 3703 |
|
|
| 3704 |
|
|
| 3705 |
DUPLICATE SUBPATTERN NUMBERS |
DUPLICATE SUBPATTERN NUMBERS |
| 3706 |
|
|
| 3707 |
Perl 5.10 introduced a feature whereby each alternative in a subpattern |
Perl 5.10 introduced a feature whereby each alternative in a subpattern |
| 3708 |
uses the same numbers for its capturing parentheses. Such a subpattern |
uses the same numbers for its capturing parentheses. Such a subpattern |
| 3709 |
starts with (?| and is itself a non-capturing subpattern. For example, |
starts with (?| and is itself a non-capturing subpattern. For example, |
| 3710 |
consider this pattern: |
consider this pattern: |
| 3711 |
|
|
| 3712 |
(?|(Sat)ur|(Sun))day |
(?|(Sat)ur|(Sun))day |
| 3713 |
|
|
| 3714 |
Because the two alternatives are inside a (?| group, both sets of cap- |
Because the two alternatives are inside a (?| group, both sets of cap- |
| 3715 |
turing parentheses are numbered one. Thus, when the pattern matches, |
turing parentheses are numbered one. Thus, when the pattern matches, |
| 3716 |
you can look at captured substring number one, whichever alternative |
you can look at captured substring number one, whichever alternative |
| 3717 |
matched. This construct is useful when you want to capture part, but |
matched. This construct is useful when you want to capture part, but |
| 3718 |
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- |
| 3719 |
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 |
| 3720 |
each branch. The numbers of any capturing buffers that follow the sub- |
each branch. The numbers of any capturing buffers that follow the sub- |
| 3721 |
pattern start after the highest number used in any branch. The follow- |
pattern start after the highest number used in any branch. The follow- |
| 3722 |
ing example is taken from the Perl documentation. The numbers under- |
ing example is taken from the Perl documentation. The numbers under- |
| 3723 |
neath show in which buffer the captured content will be stored. |
neath show in which buffer the captured content will be stored. |
| 3724 |
|
|
| 3725 |
# before ---------------branch-reset----------- after |
# before ---------------branch-reset----------- after |
| 3726 |
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
| 3727 |
# 1 2 2 3 2 3 4 |
# 1 2 2 3 2 3 4 |
| 3728 |
|
|
| 3729 |
A backreference or a recursive call to a numbered subpattern always |
A backreference or a recursive call to a numbered subpattern always |
| 3730 |
refers to the first one in the pattern with the given number. |
refers to the first one in the pattern with the given number. |
| 3731 |
|
|
| 3732 |
An alternative approach to using this "branch reset" feature is to use |
An alternative approach to using this "branch reset" feature is to use |
| 3733 |
duplicate named subpatterns, as described in the next section. |
duplicate named subpatterns, as described in the next section. |
| 3734 |
|
|
| 3735 |
|
|
| 3736 |
NAMED SUBPATTERNS |
NAMED SUBPATTERNS |
| 3737 |
|
|
| 3738 |
Identifying capturing parentheses by number is simple, but it can be |
Identifying capturing parentheses by number is simple, but it can be |
| 3739 |
very hard to keep track of the numbers in complicated regular expres- |
very hard to keep track of the numbers in complicated regular expres- |
| 3740 |
sions. Furthermore, if an expression is modified, the numbers may |
sions. Furthermore, if an expression is modified, the numbers may |
| 3741 |
change. To help with this difficulty, PCRE supports the naming of sub- |
change. To help with this difficulty, PCRE supports the naming of sub- |
| 3742 |
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 |
| 3743 |
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 |
| 3744 |
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- |
| 3745 |
tax. |
tax. |
| 3746 |
|
|
| 3747 |
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>...) |
| 3748 |
or (?'name'...) as in Perl, or (?P<name>...) as in Python. References |
or (?'name'...) as in Perl, or (?P<name>...) as in Python. References |
| 3749 |
to capturing parentheses from other parts of the pattern, such as back- |
to capturing parentheses from other parts of the pattern, such as back- |
| 3750 |
references, recursion, and conditions, can be made by name as well as |
references, recursion, and conditions, can be made by name as well as |
| 3751 |
by number. |
by number. |
| 3752 |
|
|
| 3753 |
Names consist of up to 32 alphanumeric characters and underscores. |
Names consist of up to 32 alphanumeric characters and underscores. |
| 3754 |
Named capturing parentheses are still allocated numbers as well as |
Named capturing parentheses are still allocated numbers as well as |
| 3755 |
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 |
| 3756 |
function calls for extracting the name-to-number translation table from |
function calls for extracting the name-to-number translation table from |
| 3757 |
a compiled pattern. There is also a convenience function for extracting |
a compiled pattern. There is also a convenience function for extracting |
| 3758 |
a captured substring by name. |
a captured substring by name. |
| 3759 |
|
|
| 3760 |
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 |
| 3761 |
to relax this constraint by setting the PCRE_DUPNAMES option at compile |
to relax this constraint by setting the PCRE_DUPNAMES option at compile |
| 3762 |
time. This can be useful for patterns where only one instance of the |
time. This can be useful for patterns where only one instance of the |
| 3763 |
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 |
| 3764 |
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 |
| 3765 |
both cases you want to extract the abbreviation. This pattern (ignoring |
both cases you want to extract the abbreviation. This pattern (ignoring |
| 3766 |
the line breaks) does the job: |
the line breaks) does the job: |
| 3767 |
|
|
| 3771 |
(?<DN>Thu)(?:rsday)?| |
(?<DN>Thu)(?:rsday)?| |
| 3772 |
(?<DN>Sat)(?:urday)? |
(?<DN>Sat)(?:urday)? |
| 3773 |
|
|
| 3774 |
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 |
| 3775 |
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 |
| 3776 |
reset" subpattern, as described in the previous section.) |
reset" subpattern, as described in the previous section.) |
| 3777 |
|
|
| 3778 |
The convenience function for extracting the data by name returns the |
The convenience function for extracting the data by name returns the |
| 3779 |
substring for the first (and in this example, the only) subpattern of |
substring for the first (and in this example, the only) subpattern of |
| 3780 |
that name that matched. This saves searching to find which numbered |
that name that matched. This saves searching to find which numbered |
| 3781 |
subpattern it was. If you make a reference to a non-unique named sub- |
subpattern it was. If you make a reference to a non-unique named sub- |
| 3782 |
pattern from elsewhere in the pattern, the one that corresponds to the |
pattern from elsewhere in the pattern, the one that corresponds to the |
| 3783 |
lowest number is used. For further details of the interfaces for han- |
lowest number is used. For further details of the interfaces for han- |
| 3784 |
dling named subpatterns, see the pcreapi documentation. |
dling named subpatterns, see the pcreapi documentation. |
| 3785 |
|
|
| 3786 |
|
|
| 3787 |
REPETITION |
REPETITION |
| 3788 |
|
|
| 3789 |
Repetition is specified by quantifiers, which can follow any of the |
Repetition is specified by quantifiers, which can follow any of the |
| 3790 |
following items: |
following items: |
| 3791 |
|
|
| 3792 |
a literal data character |
a literal data character |
| 3799 |
a back reference (see next section) |
a back reference (see next section) |
| 3800 |
a parenthesized subpattern (unless it is an assertion) |
a parenthesized subpattern (unless it is an assertion) |
| 3801 |
|
|
| 3802 |
The general repetition quantifier specifies a minimum and maximum num- |
The general repetition quantifier specifies a minimum and maximum num- |
| 3803 |
ber of permitted matches, by giving the two numbers in curly brackets |
ber of permitted matches, by giving the two numbers in curly brackets |
| 3804 |
(braces), separated by a comma. The numbers must be less than 65536, |
(braces), separated by a comma. The numbers must be less than 65536, |
| 3805 |
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: |
| 3806 |
|
|
| 3807 |
z{2,4} |
z{2,4} |
| 3808 |
|
|
| 3809 |
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 |
| 3810 |
special character. If the second number is omitted, but the comma is |
special character. If the second number is omitted, but the comma is |
| 3811 |
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 |
| 3812 |
are both omitted, the quantifier specifies an exact number of required |
are both omitted, the quantifier specifies an exact number of required |
| 3813 |
matches. Thus |
matches. Thus |
| 3814 |
|
|
| 3815 |
[aeiou]{3,} |
[aeiou]{3,} |
| 3818 |
|
|
| 3819 |
\d{8} |
\d{8} |
| 3820 |
|
|
| 3821 |
matches exactly 8 digits. An opening curly bracket that appears in a |
matches exactly 8 digits. An opening curly bracket that appears in a |
| 3822 |
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 |
| 3823 |
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- |
| 3824 |
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. |
| 3825 |
|
|
| 3826 |
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 |
| 3827 |
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- |
| 3828 |
acters, each of which is represented by a two-byte sequence. Similarly, |
acters, each of which is represented by a two-byte sequence. Similarly, |
| 3829 |
when Unicode property support is available, \X{3} matches three Unicode |
when Unicode property support is available, \X{3} matches three Unicode |
| 3830 |
extended sequences, each of which may be several bytes long (and they |
extended sequences, each of which may be several bytes long (and they |
| 3831 |
may be of different lengths). |
may be of different lengths). |
| 3832 |
|
|
| 3833 |
The quantifier {0} is permitted, causing the expression to behave as if |
The quantifier {0} is permitted, causing the expression to behave as if |
| 3834 |
the previous item and the quantifier were not present. |
the previous item and the quantifier were not present. |
| 3835 |
|
|
| 3836 |
For convenience, the three most common quantifiers have single-charac- |
For convenience, the three most common quantifiers have single-charac- |
| 3837 |
ter abbreviations: |
ter abbreviations: |
| 3838 |
|
|
| 3839 |
* is equivalent to {0,} |
* is equivalent to {0,} |
| 3840 |
+ is equivalent to {1,} |
+ is equivalent to {1,} |
| 3841 |
? is equivalent to {0,1} |
? is equivalent to {0,1} |
| 3842 |
|
|
| 3843 |
It is possible to construct infinite loops by following a subpattern |
It is possible to construct infinite loops by following a subpattern |
| 3844 |
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, |
| 3845 |
for example: |
for example: |
| 3846 |
|
|
| 3847 |
(a?)* |
(a?)* |
| 3848 |
|
|
| 3849 |
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 |
| 3850 |
for such patterns. However, because there are cases where this can be |
for such patterns. However, because there are cases where this can be |
| 3851 |
useful, such patterns are now accepted, but if any repetition of the |
useful, such patterns are now accepted, but if any repetition of the |
| 3852 |
subpattern does in fact match no characters, the loop is forcibly bro- |
subpattern does in fact match no characters, the loop is forcibly bro- |
| 3853 |
ken. |
ken. |
| 3854 |
|
|
| 3855 |
By default, the quantifiers are "greedy", that is, they match as much |
By default, the quantifiers are "greedy", that is, they match as much |
| 3856 |
as possible (up to the maximum number of permitted times), without |
as possible (up to the maximum number of permitted times), without |
| 3857 |
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 |
| 3858 |
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 |
| 3859 |
appear between /* and */ and within the comment, individual * and / |
appear between /* and */ and within the comment, individual * and / |
| 3860 |
characters may appear. An attempt to match C comments by applying the |
characters may appear. An attempt to match C comments by applying the |
| 3861 |
pattern |
pattern |
| 3862 |
|
|
| 3863 |
/\*.*\*/ |
/\*.*\*/ |
| 3866 |
|
|
| 3867 |
/* first comment */ not comment /* second comment */ |
/* first comment */ not comment /* second comment */ |
| 3868 |
|
|
| 3869 |
fails, because it matches the entire string owing to the greediness of |
fails, because it matches the entire string owing to the greediness of |
| 3870 |
the .* item. |
the .* item. |
| 3871 |
|
|
| 3872 |
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 |
| 3873 |
be greedy, and instead matches the minimum number of times possible, so |
be greedy, and instead matches the minimum number of times possible, so |
| 3874 |
the pattern |
the pattern |
| 3875 |
|
|
| 3876 |
/\*.*?\*/ |
/\*.*?\*/ |
| 3877 |
|
|
| 3878 |
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 |
| 3879 |
quantifiers is not otherwise changed, just the preferred number of |
quantifiers is not otherwise changed, just the preferred number of |
| 3880 |
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 |
| 3881 |
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 |
| 3882 |
appear doubled, as in |
appear doubled, as in |
| 3883 |
|
|
| 3884 |
\d??\d |
\d??\d |
| 3886 |
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 |
| 3887 |
only way the rest of the pattern matches. |
only way the rest of the pattern matches. |
| 3888 |
|
|
| 3889 |
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 |
| 3890 |
Perl), the quantifiers are not greedy by default, but individual ones |
Perl), the quantifiers are not greedy by default, but individual ones |
| 3891 |
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 |
| 3892 |
words, it inverts the default behaviour. |
words, it inverts the default behaviour. |
| 3893 |
|
|
| 3894 |
When a parenthesized subpattern is quantified with a minimum repeat |
When a parenthesized subpattern is quantified with a minimum repeat |
| 3895 |
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 |
| 3896 |
required for the compiled pattern, in proportion to the size of the |
required for the compiled pattern, in proportion to the size of the |
| 3897 |
minimum or maximum. |
minimum or maximum. |
| 3898 |
|
|
| 3899 |
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- |
| 3900 |
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, |
| 3901 |
the pattern is implicitly anchored, because whatever follows will be |
the pattern is implicitly anchored, because whatever follows will be |
| 3902 |
tried against every character position in the subject string, so there |
tried against every character position in the subject string, so there |
| 3903 |
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 |
| 3904 |
first. PCRE normally treats such a pattern as though it were preceded |
first. PCRE normally treats such a pattern as though it were preceded |
| 3905 |
by \A. |
by \A. |
| 3906 |
|
|
| 3907 |
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- |
| 3908 |
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- |
| 3909 |
mization, or alternatively using ^ to indicate anchoring explicitly. |
mization, or alternatively using ^ to indicate anchoring explicitly. |
| 3910 |
|
|
| 3911 |
However, there is one situation where the optimization cannot be used. |
However, there is one situation where the optimization cannot be used. |
| 3912 |
When .* is inside capturing parentheses that are the subject of a |
When .* is inside capturing parentheses that are the subject of a |
| 3913 |
backreference elsewhere in the pattern, a match at the start may fail |
backreference elsewhere in the pattern, a match at the start may fail |
| 3914 |
where a later one succeeds. Consider, for example: |
where a later one succeeds. Consider, for example: |
| 3915 |
|
|
| 3916 |
(.*)abc\1 |
(.*)abc\1 |
| 3917 |
|
|
| 3918 |
If the subject is "xyz123abc123" the match point is the fourth charac- |
If the subject is "xyz123abc123" the match point is the fourth charac- |
| 3919 |
ter. For this reason, such a pattern is not implicitly anchored. |
ter. For this reason, such a pattern is not implicitly anchored. |
| 3920 |
|
|
| 3921 |
When a capturing subpattern is repeated, the value captured is the sub- |
When a capturing subpattern is repeated, the value captured is the sub- |
| 3924 |
(tweedle[dume]{3}\s*)+ |
(tweedle[dume]{3}\s*)+ |
| 3925 |
|
|
| 3926 |
has matched "tweedledum tweedledee" the value of the captured substring |
has matched "tweedledum tweedledee" the value of the captured substring |
| 3927 |
is "tweedledee". However, if there are nested capturing subpatterns, |
is "tweedledee". However, if there are nested capturing subpatterns, |
| 3928 |
the corresponding captured values may have been set in previous itera- |
the corresponding captured values may have been set in previous itera- |
| 3929 |
tions. For example, after |
tions. For example, after |
| 3930 |
|
|
| 3931 |
/(a|(b))+/ |
/(a|(b))+/ |
| 3935 |
|
|
| 3936 |
ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS |
ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS |
| 3937 |
|
|
| 3938 |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
| 3939 |
repetition, failure of what follows normally causes the repeated item |
repetition, failure of what follows normally causes the repeated item |
| 3940 |
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 |
| 3941 |
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, |
| 3942 |
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 |
| 3943 |
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 |
| 3944 |
no point in carrying on. |
no point in carrying on. |
| 3945 |
|
|
| 3946 |
Consider, for example, the pattern \d+foo when applied to the subject |
Consider, for example, the pattern \d+foo when applied to the subject |
| 3947 |
line |
line |
| 3948 |
|
|
| 3949 |
123456bar |
123456bar |
| 3950 |
|
|
| 3951 |
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 |
| 3952 |
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 |
| 3953 |
\d+ item, and then with 4, and so on, before ultimately failing. |
\d+ item, and then with 4, and so on, before ultimately failing. |
| 3954 |
"Atomic grouping" (a term taken from Jeffrey Friedl's book) provides |
"Atomic grouping" (a term taken from Jeffrey Friedl's book) provides |
| 3955 |
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 |
| 3956 |
to be re-evaluated in this way. |
to be re-evaluated in this way. |
| 3957 |
|
|
| 3958 |
If we use atomic grouping for the previous example, the matcher gives |
If we use atomic grouping for the previous example, the matcher gives |
| 3959 |
up immediately on failing to match "foo" the first time. The notation |
up immediately on failing to match "foo" the first time. The notation |
| 3960 |
is a kind of special parenthesis, starting with (?> as in this example: |
is a kind of special parenthesis, starting with (?> as in this example: |
| 3961 |
|
|
| 3962 |
(?>\d+)foo |
(?>\d+)foo |
| 3963 |
|
|
| 3964 |
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- |
| 3965 |
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 |
| 3966 |
prevented from backtracking into it. Backtracking past it to previous |
prevented from backtracking into it. Backtracking past it to previous |
| 3967 |
items, however, works as normal. |
items, however, works as normal. |
| 3968 |
|
|
| 3969 |
An alternative description is that a subpattern of this type matches |
An alternative description is that a subpattern of this type matches |
| 3970 |
the string of characters that an identical standalone pattern would |
the string of characters that an identical standalone pattern would |
| 3971 |
match, if anchored at the current point in the subject string. |
match, if anchored at the current point in the subject string. |
| 3972 |
|
|
| 3973 |
Atomic grouping subpatterns are not capturing subpatterns. Simple cases |
Atomic grouping subpatterns are not capturing subpatterns. Simple cases |
| 3974 |
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 |
| 3975 |
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- |
| 3976 |
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 |
| 3977 |
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 |
| 3978 |
digits. |
digits. |
| 3979 |
|
|
| 3980 |
Atomic groups in general can of course contain arbitrarily complicated |
Atomic groups in general can of course contain arbitrarily complicated |
| 3981 |
subpatterns, and can be nested. However, when the subpattern for an |
subpatterns, and can be nested. However, when the subpattern for an |
| 3982 |
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 |
| 3983 |
simpler notation, called a "possessive quantifier" can be used. This |
simpler notation, called a "possessive quantifier" can be used. This |
| 3984 |
consists of an additional + character following a quantifier. Using |
consists of an additional + character following a quantifier. Using |
| 3985 |
this notation, the previous example can be rewritten as |
this notation, the previous example can be rewritten as |
| 3986 |
|
|
| 3987 |
\d++foo |
\d++foo |
| 3991 |
|
|
| 3992 |
(abc|xyz){2,3}+ |
(abc|xyz){2,3}+ |
| 3993 |
|
|
| 3994 |
Possessive quantifiers are always greedy; the setting of the |
Possessive quantifiers are always greedy; the setting of the |
| 3995 |
PCRE_UNGREEDY option is ignored. They are a convenient notation for the |
PCRE_UNGREEDY option is ignored. They are a convenient notation for the |
| 3996 |
simpler forms of atomic group. However, there is no difference in the |
simpler forms of atomic group. However, there is no difference in the |
| 3997 |
meaning of a possessive quantifier and the equivalent atomic group, |
meaning of a possessive quantifier and the equivalent atomic group, |
| 3998 |
though there may be a performance difference; possessive quantifiers |
though there may be a performance difference; possessive quantifiers |
| 3999 |
should be slightly faster. |
should be slightly faster. |
| 4000 |
|
|
| 4001 |
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- |
| 4002 |
tax. Jeffrey Friedl originated the idea (and the name) in the first |
tax. Jeffrey Friedl originated the idea (and the name) in the first |
| 4003 |
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 |
| 4004 |
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 |
| 4005 |
found its way into Perl at release 5.10. |
found its way into Perl at release 5.10. |
| 4006 |
|
|
| 4007 |
PCRE has an optimization that automatically "possessifies" certain sim- |
PCRE has an optimization that automatically "possessifies" certain sim- |
| 4008 |
ple pattern constructs. For example, the sequence A+B is treated as |
ple pattern constructs. For example, the sequence A+B is treated as |
| 4009 |
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 |
| 4010 |
when B must follow. |
when B must follow. |
| 4011 |
|
|
| 4012 |
When a pattern contains an unlimited repeat inside a subpattern that |
When a pattern contains an unlimited repeat inside a subpattern that |
| 4013 |
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 |
| 4014 |
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 |
| 4015 |
very long time indeed. The pattern |
very long time indeed. The pattern |
| 4016 |
|
|
| 4017 |
(\D+|<\d+>)*[!?] |
(\D+|<\d+>)*[!?] |
| 4018 |
|
|
| 4019 |
matches an unlimited number of substrings that either consist of non- |
matches an unlimited number of substrings that either consist of non- |
| 4020 |
digits, or digits enclosed in <>, followed by either ! or ?. When it |
digits, or digits enclosed in <>, followed by either ! or ?. When it |
| 4021 |
matches, it runs quickly. However, if it is applied to |
matches, it runs quickly. However, if it is applied to |
| 4022 |
|
|
| 4023 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 4024 |
|
|
| 4025 |
it takes a long time before reporting failure. This is because the |
it takes a long time before reporting failure. This is because the |
| 4026 |
string can be divided between the internal \D+ repeat and the external |
string can be divided between the internal \D+ repeat and the external |
| 4027 |
* 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 |
| 4028 |
example uses [!?] rather than a single character at the end, because |
example uses [!?] rather than a single character at the end, because |
| 4029 |
both PCRE and Perl have an optimization that allows for fast failure |
both PCRE and Perl have an optimization that allows for fast failure |
| 4030 |
when a single character is used. They remember the last single charac- |
when a single character is used. They remember the last single charac- |
| 4031 |
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 |
| 4032 |
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 |
| 4033 |
group, like this: |
group, like this: |
| 4034 |
|
|
| 4035 |
((?>\D+)|<\d+>)*[!?] |
((?>\D+)|<\d+>)*[!?] |
| 4036 |
|
|
| 4037 |
sequences of non-digits cannot be broken, and failure happens quickly. |
sequences of non-digits cannot be broken, and failure happens quickly. |
| 4038 |
|
|
| 4039 |
|
|
| 4040 |
BACK REFERENCES |
BACK REFERENCES |
| 4041 |
|
|
| 4042 |
Outside a character class, a backslash followed by a digit greater than |
Outside a character class, a backslash followed by a digit greater than |
| 4043 |
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- |
| 4044 |
pattern earlier (that is, to its left) in the pattern, provided there |
pattern earlier (that is, to its left) in the pattern, provided there |
| 4045 |
have been that many previous capturing left parentheses. |
have been that many previous capturing left parentheses. |
| 4046 |
|
|
| 4047 |
However, if the decimal number following the backslash is less than 10, |
However, if the decimal number following the backslash is less than 10, |
| 4048 |
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 |
| 4049 |
there are not that many capturing left parentheses in the entire pat- |
there are not that many capturing left parentheses in the entire pat- |
| 4050 |
tern. In other words, the parentheses that are referenced need not be |
tern. In other words, the parentheses that are referenced need not be |
| 4051 |
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 |
| 4052 |
reference" of this type can make sense when a repetition is involved |
reference" of this type can make sense when a repetition is involved |
| 4053 |
and the subpattern to the right has participated in an earlier itera- |
and the subpattern to the right has participated in an earlier itera- |
| 4054 |
tion. |
tion. |
| 4055 |
|
|
| 4056 |
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 |
| 4057 |
subpattern whose number is 10 or more using this syntax because a |
subpattern whose number is 10 or more using this syntax because a |
| 4058 |
sequence such as \50 is interpreted as a character defined in octal. |
sequence such as \50 is interpreted as a character defined in octal. |
| 4059 |
See the subsection entitled "Non-printing characters" above for further |
See the subsection entitled "Non-printing characters" above for further |
| 4060 |
details of the handling of digits following a backslash. There is no |
details of the handling of digits following a backslash. There is no |
| 4061 |
such problem when named parentheses are used. A back reference to any |
such problem when named parentheses are used. A back reference to any |
| 4062 |
subpattern is possible using named parentheses (see below). |
subpattern is possible using named parentheses (see below). |
| 4063 |
|
|
| 4064 |
Another way of avoiding the ambiguity inherent in the use of digits |
Another way of avoiding the ambiguity inherent in the use of digits |
| 4065 |
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- |
| 4066 |
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 |
| 4067 |
unsigned number or a negative number, optionally enclosed in braces. |
unsigned number or a negative number, optionally enclosed in braces. |
| 4068 |
These examples are all identical: |
These examples are all identical: |
| 4069 |
|
|
| 4070 |
(ring), \1 |
(ring), \1 |
| 4071 |
(ring), \g1 |
(ring), \g1 |
| 4072 |
(ring), \g{1} |
(ring), \g{1} |
| 4073 |
|
|
| 4074 |
An unsigned number specifies an absolute reference without the ambigu- |
An unsigned number specifies an absolute reference without the ambigu- |
| 4075 |
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 |
| 4076 |
digits follow the reference. A negative number is a relative reference. |
digits follow the reference. A negative number is a relative reference. |
| 4077 |
Consider this example: |
Consider this example: |
| 4079 |
(abc(def)ghi)\g{-1} |
(abc(def)ghi)\g{-1} |
| 4080 |
|
|
| 4081 |
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- |
| 4082 |
ing subpattern before \g, that is, is it equivalent to \2. Similarly, |
ing subpattern before \g, that is, is it equivalent to \2. Similarly, |
| 4083 |
\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 |
| 4084 |
helpful in long patterns, and also in patterns that are created by |
helpful in long patterns, and also in patterns that are created by |
| 4085 |
joining together fragments that contain references within themselves. |
joining together fragments that contain references within themselves. |
| 4086 |
|
|
| 4087 |
A back reference matches whatever actually matched the capturing sub- |
A back reference matches whatever actually matched the capturing sub- |
| 4088 |
pattern in the current subject string, rather than anything matching |
pattern in the current subject string, rather than anything matching |
| 4089 |
the subpattern itself (see "Subpatterns as subroutines" below for a way |
the subpattern itself (see "Subpatterns as subroutines" below for a way |
| 4090 |
of doing that). So the pattern |
of doing that). So the pattern |
| 4091 |
|
|
| 4092 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
| 4093 |
|
|
| 4094 |
matches "sense and sensibility" and "response and responsibility", but |
matches "sense and sensibility" and "response and responsibility", but |
| 4095 |
not "sense and responsibility". If caseful matching is in force at the |
not "sense and responsibility". If caseful matching is in force at the |
| 4096 |
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- |
| 4097 |
ple, |
ple, |
| 4098 |
|
|
| 4099 |
((?i)rah)\s+\1 |
((?i)rah)\s+\1 |
| 4100 |
|
|
| 4101 |
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 |
| 4102 |
original capturing subpattern is matched caselessly. |
original capturing subpattern is matched caselessly. |
| 4103 |
|
|
| 4104 |
There are several different ways of writing back references to named |
There are several different ways of writing back references to named |
| 4105 |
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 |
| 4106 |
\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 |
| 4107 |
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 |
| 4108 |
and named references, is also supported. We could rewrite the above |
and named references, is also supported. We could rewrite the above |
| 4109 |
example in any of the following ways: |
example in any of the following ways: |
| 4110 |
|
|
| 4111 |
(?<p1>(?i)rah)\s+\k<p1> |
(?<p1>(?i)rah)\s+\k<p1> |
| 4113 |
(?P<p1>(?i)rah)\s+(?P=p1) |
(?P<p1>(?i)rah)\s+(?P=p1) |
| 4114 |
(?<p1>(?i)rah)\s+\g{p1} |
(?<p1>(?i)rah)\s+\g{p1} |
| 4115 |
|
|
| 4116 |
A subpattern that is referenced by name may appear in the pattern |
A subpattern that is referenced by name may appear in the pattern |
| 4117 |
before or after the reference. |
before or after the reference. |
| 4118 |
|
|
| 4119 |
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 |
| 4120 |
subpattern has not actually been used in a particular match, any back |
subpattern has not actually been used in a particular match, any back |
| 4121 |
references to it always fail. For example, the pattern |
references to it always fail. For example, the pattern |
| 4122 |
|
|
| 4123 |
(a|(bc))\2 |
(a|(bc))\2 |
| 4124 |
|
|
| 4125 |
always fails if it starts to match "a" rather than "bc". Because there |
always fails if it starts to match "a" rather than "bc". Because there |
| 4126 |
may be many capturing parentheses in a pattern, all digits following |
may be many capturing parentheses in a pattern, all digits following |
| 4127 |
the backslash are taken as part of a potential back reference number. |
the backslash are taken as part of a potential back reference number. |
| 4128 |
If the pattern continues with a digit character, some delimiter must be |
If the pattern continues with a digit character, some delimiter must be |
| 4129 |
used to terminate the back reference. If the PCRE_EXTENDED option is |
used to terminate the back reference. If the PCRE_EXTENDED option is |
| 4130 |
set, this can be whitespace. Otherwise an empty comment (see "Com- |
set, this can be whitespace. Otherwise an empty comment (see "Com- |
| 4131 |
ments" below) can be used. |
ments" below) can be used. |
| 4132 |
|
|
| 4133 |
A back reference that occurs inside the parentheses to which it refers |
A back reference that occurs inside the parentheses to which it refers |
| 4134 |
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 |
| 4135 |
matches. However, such references can be useful inside repeated sub- |
matches. However, such references can be useful inside repeated sub- |
| 4136 |
patterns. For example, the pattern |
patterns. For example, the pattern |
| 4137 |
|
|
| 4138 |
(a|b\1)+ |
(a|b\1)+ |
| 4139 |
|
|
| 4140 |
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- |
| 4141 |
ation of the subpattern, the back reference matches the character |
ation of the subpattern, the back reference matches the character |
| 4142 |
string corresponding to the previous iteration. In order for this to |
string corresponding to the previous iteration. In order for this to |
| 4143 |
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 |
| 4144 |
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 |
| 4145 |
the example above, or by a quantifier with a minimum of zero. |
the example above, or by a quantifier with a minimum of zero. |
| 4146 |
|
|
| 4147 |
|
|
| 4148 |
ASSERTIONS |
ASSERTIONS |
| 4149 |
|
|
| 4150 |
An assertion is a test on the characters following or preceding the |
An assertion is a test on the characters following or preceding the |
| 4151 |
current matching point that does not actually consume any characters. |
current matching point that does not actually consume any characters. |
| 4152 |
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 |
| 4153 |
described above. |
described above. |
| 4154 |
|
|
| 4155 |
More complicated assertions are coded as subpatterns. There are two |
More complicated assertions are coded as subpatterns. There are two |
| 4156 |
kinds: those that look ahead of the current position in the subject |
kinds: those that look ahead of the current position in the subject |
| 4157 |
string, and those that look behind it. An assertion subpattern is |
string, and those that look behind it. An assertion subpattern is |
| 4158 |
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 |
| 4159 |
matching position to be changed. |
matching position to be changed. |
| 4160 |
|
|
| 4161 |
Assertion subpatterns are not capturing subpatterns, and may not be |
Assertion subpatterns are not capturing subpatterns, and may not be |
| 4162 |
repeated, because it makes no sense to assert the same thing several |
repeated, because it makes no sense to assert the same thing several |
| 4163 |
times. If any kind of assertion contains capturing subpatterns within |
times. If any kind of assertion contains capturing subpatterns within |
| 4164 |
it, these are counted for the purposes of numbering the capturing sub- |
it, these are counted for the purposes of numbering the capturing sub- |
| 4165 |
patterns in the whole pattern. However, substring capturing is carried |
patterns in the whole pattern. However, substring capturing is carried |
| 4166 |
out only for positive assertions, because it does not make sense for |
out only for positive assertions, because it does not make sense for |
| 4167 |
negative assertions. |
negative assertions. |
| 4168 |
|
|
| 4169 |
Lookahead assertions |
Lookahead assertions |
| 4173 |
|
|
| 4174 |
\w+(?=;) |
\w+(?=;) |
| 4175 |
|
|
| 4176 |
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- |
| 4177 |
colon in the match, and |
colon in the match, and |
| 4178 |
|
|
| 4179 |
foo(?!bar) |
foo(?!bar) |
| 4180 |
|
|
| 4181 |
matches any occurrence of "foo" that is not followed by "bar". Note |
matches any occurrence of "foo" that is not followed by "bar". Note |
| 4182 |
that the apparently similar pattern |
that the apparently similar pattern |
| 4183 |
|
|
| 4184 |
(?!foo)bar |
(?!foo)bar |
| 4185 |
|
|
| 4186 |
does not find an occurrence of "bar" that is preceded by something |
does not find an occurrence of "bar" that is preceded by something |
| 4187 |
other than "foo"; it finds any occurrence of "bar" whatsoever, because |
other than "foo"; it finds any occurrence of "bar" whatsoever, because |
| 4188 |
the assertion (?!foo) is always true when the next three characters are |
the assertion (?!foo) is always true when the next three characters are |
| 4189 |
"bar". A lookbehind assertion is needed to achieve the other effect. |
"bar". A lookbehind assertion is needed to achieve the other effect. |
| 4190 |
|
|
| 4191 |
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 |
| 4192 |
most convenient way to do it is with (?!) because an empty string |
most convenient way to do it is with (?!) because an empty string |
| 4193 |
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 |
| 4194 |
string must always fail. |
string must always fail. |
| 4195 |
|
|
| 4196 |
Lookbehind assertions |
Lookbehind assertions |
| 4197 |
|
|
| 4198 |
Lookbehind assertions start with (?<= for positive assertions and (?<! |
Lookbehind assertions start with (?<= for positive assertions and (?<! |
| 4199 |
for negative assertions. For example, |
for negative assertions. For example, |
| 4200 |
|
|
| 4201 |
(?<!foo)bar |
(?<!foo)bar |
| 4202 |
|
|
| 4203 |
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 |
| 4204 |
contents of a lookbehind assertion are restricted such that all the |
contents of a lookbehind assertion are restricted such that all the |
| 4205 |
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- |
| 4206 |
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 |
| 4207 |
fixed length. Thus |
fixed length. Thus |
| 4208 |
|
|
| 4209 |
(?<=bullock|donkey) |
(?<=bullock|donkey) |
| 4212 |
|
|
| 4213 |
(?<!dogs?|cats?) |
(?<!dogs?|cats?) |
| 4214 |
|
|
| 4215 |
causes an error at compile time. Branches that match different length |
causes an error at compile time. Branches that match different length |
| 4216 |
strings are permitted only at the top level of a lookbehind assertion. |
strings are permitted only at the top level of a lookbehind assertion. |
| 4217 |
This is an extension compared with Perl (at least for 5.8), which |
This is an extension compared with Perl (at least for 5.8), which |
| 4218 |
requires all branches to match the same length of string. An assertion |
requires all branches to match the same length of string. An assertion |
| 4219 |
such as |
such as |
| 4220 |
|
|
| 4221 |
(?<=ab(c|de)) |
(?<=ab(c|de)) |
| 4222 |
|
|
| 4223 |
is not permitted, because its single top-level branch can match two |
is not permitted, because its single top-level branch can match two |
| 4224 |
different lengths, but it is acceptable if rewritten to use two top- |
different lengths, but it is acceptable if rewritten to use two top- |
| 4225 |
level branches: |
level branches: |
| 4226 |
|
|
| 4227 |
(?<=abc|abde) |
(?<=abc|abde) |
| 4228 |
|
|
| 4229 |
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 |
| 4230 |
instead of a lookbehind assertion; this is not restricted to a fixed- |
instead of a lookbehind assertion; this is not restricted to a fixed- |
| 4231 |
length. |
length. |
| 4232 |
|
|
| 4233 |
The implementation of lookbehind assertions is, for each alternative, |
The implementation of lookbehind assertions is, for each alternative, |
| 4234 |
to temporarily move the current position back by the fixed length and |
to temporarily move the current position back by the fixed length and |
| 4235 |
then try to match. If there are insufficient characters before the cur- |
then try to match. If there are insufficient characters before the cur- |
| 4236 |
rent position, the assertion fails. |
rent position, the assertion fails. |
| 4237 |
|
|
| 4238 |
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 |
| 4239 |
mode) to appear in lookbehind assertions, because it makes it impossi- |
mode) to appear in lookbehind assertions, because it makes it impossi- |
| 4240 |
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, |
| 4241 |
which can match different numbers of bytes, are also not permitted. |
which can match different numbers of bytes, are also not permitted. |
| 4242 |
|
|
| 4243 |
Possessive quantifiers can be used in conjunction with lookbehind |
Possessive quantifiers can be used in conjunction with lookbehind |
| 4244 |
assertions to specify efficient matching at the end of the subject |
assertions to specify efficient matching at the end of the subject |
| 4245 |
string. Consider a simple pattern such as |
string. Consider a simple pattern such as |
| 4246 |
|
|
| 4247 |
abcd$ |
abcd$ |
| 4248 |
|
|
| 4249 |
when applied to a long string that does not match. Because matching |
when applied to a long string that does not match. Because matching |
| 4250 |
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 |
| 4251 |
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 |
| 4252 |
pattern is specified as |
pattern is specified as |
| 4253 |
|
|
| 4254 |
^.*abcd$ |
^.*abcd$ |
| 4255 |
|
|
| 4256 |
the initial .* matches the entire string at first, but when this fails |
the initial .* matches the entire string at first, but when this fails |
| 4257 |
(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 |
| 4258 |
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 |
| 4259 |
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, |
| 4260 |
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 |
| 4261 |
|
|
| 4262 |
^.*+(?<=abcd) |
^.*+(?<=abcd) |
| 4263 |
|
|
| 4264 |
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 |
| 4265 |
entire string. The subsequent lookbehind assertion does a single test |
entire string. The subsequent lookbehind assertion does a single test |
| 4266 |
on the last four characters. If it fails, the match fails immediately. |
on the last four characters. If it fails, the match fails immediately. |
| 4267 |
For long strings, this approach makes a significant difference to the |
For long strings, this approach makes a significant difference to the |
| 4268 |
processing time. |
processing time. |
| 4269 |
|
|
| 4270 |
Using multiple assertions |
Using multiple assertions |
| 4273 |
|
|
| 4274 |
(?<=\d{3})(?<!999)foo |
(?<=\d{3})(?<!999)foo |
| 4275 |
|
|
| 4276 |
matches "foo" preceded by three digits that are not "999". Notice that |
matches "foo" preceded by three digits that are not "999". Notice that |
| 4277 |
each of the assertions is applied independently at the same point in |
each of the assertions is applied independently at the same point in |
| 4278 |
the subject string. First there is a check that the previous three |
the subject string. First there is a check that the previous three |
| 4279 |
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 |
| 4280 |
three characters are not "999". This pattern does not match "foo" pre- |
three characters are not "999". This pattern does not match "foo" pre- |
| 4281 |
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 |
| 4282 |
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- |
| 4283 |
foo". A pattern to do that is |
foo". A pattern to do that is |
| 4284 |
|
|
| 4285 |
(?<=\d{3}...)(?<!999)foo |
(?<=\d{3}...)(?<!999)foo |
| 4286 |
|
|
| 4287 |
This time the first assertion looks at the preceding six characters, |
This time the first assertion looks at the preceding six characters, |
| 4288 |
checking that the first three are digits, and then the second assertion |
checking that the first three are digits, and then the second assertion |
| 4289 |
checks that the preceding three characters are not "999". |
checks that the preceding three characters are not "999". |
| 4290 |
|
|
| 4292 |
|
|
| 4293 |
(?<=(?<!foo)bar)baz |
(?<=(?<!foo)bar)baz |
| 4294 |
|
|
| 4295 |
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 |
| 4296 |
is not preceded by "foo", while |
is not preceded by "foo", while |
| 4297 |
|
|
| 4298 |
(?<=\d{3}(?!999)...)foo |
(?<=\d{3}(?!999)...)foo |
| 4299 |
|
|
| 4300 |
is another pattern that matches "foo" preceded by three digits and any |
is another pattern that matches "foo" preceded by three digits and any |
| 4301 |
three characters that are not "999". |
three characters that are not "999". |
| 4302 |
|
|
| 4303 |
|
|
| 4304 |
CONDITIONAL SUBPATTERNS |
CONDITIONAL SUBPATTERNS |
| 4305 |
|
|
| 4306 |
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- |
| 4307 |
ditionally or to choose between two alternative subpatterns, depending |
ditionally or to choose between two alternative subpatterns, depending |
| 4308 |
on the result of an assertion, or whether a previous capturing subpat- |
on the result of an assertion, or whether a previous capturing subpat- |
| 4309 |
tern matched or not. The two possible forms of conditional subpattern |
tern matched or not. The two possible forms of conditional subpattern |
| 4310 |
are |
are |
| 4311 |
|
|
| 4312 |
(?(condition)yes-pattern) |
(?(condition)yes-pattern) |
| 4313 |
(?(condition)yes-pattern|no-pattern) |
(?(condition)yes-pattern|no-pattern) |
| 4314 |
|
|
| 4315 |
If the condition is satisfied, the yes-pattern is used; otherwise the |
If the condition is satisfied, the yes-pattern is used; otherwise the |
| 4316 |
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- |
| 4317 |
tives in the subpattern, a compile-time error occurs. |
tives in the subpattern, a compile-time error occurs. |
| 4318 |
|
|
| 4319 |
There are four kinds of condition: references to subpatterns, refer- |
There are four kinds of condition: references to subpatterns, refer- |
| 4320 |
ences to recursion, a pseudo-condition called DEFINE, and assertions. |
ences to recursion, a pseudo-condition called DEFINE, and assertions. |
| 4321 |
|
|
| 4322 |
Checking for a used subpattern by number |
Checking for a used subpattern by number |
| 4323 |
|
|
| 4324 |
If the text between the parentheses consists of a sequence of digits, |
If the text between the parentheses consists of a sequence of digits, |
| 4325 |
the condition is true if the capturing subpattern of that number has |
the condition is true if the capturing subpattern of that number has |
| 4326 |
previously matched. An alternative notation is to precede the digits |
previously matched. An alternative notation is to precede the digits |
| 4327 |
with a plus or minus sign. In this case, the subpattern number is rela- |
with a plus or minus sign. In this case, the subpattern number is rela- |
| 4328 |
tive rather than absolute. The most recently opened parentheses can be |
tive rather than absolute. The most recently opened parentheses can be |
| 4329 |
referenced by (?(-1), the next most recent by (?(-2), and so on. In |
referenced by (?(-1), the next most recent by (?(-2), and so on. In |
| 4330 |
looping constructs it can also make sense to refer to subsequent groups |
looping constructs it can also make sense to refer to subsequent groups |
| 4331 |
with constructs such as (?(+2). |
with constructs such as (?(+2). |
| 4332 |
|
|
| 4333 |
Consider the following pattern, which contains non-significant white |
Consider the following pattern, which contains non-significant white |
| 4334 |
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 |
| 4335 |
divide it into three parts for ease of discussion: |
divide it into three parts for ease of discussion: |
| 4336 |
|
|
| 4337 |
( \( )? [^()]+ (?(1) \) ) |
( \( )? [^()]+ (?(1) \) ) |
| 4338 |
|
|
| 4339 |
The first part matches an optional opening parenthesis, and if that |
The first part matches an optional opening parenthesis, and if that |
| 4340 |
character is present, sets it as the first captured substring. The sec- |
character is present, sets it as the first captured substring. The sec- |
| 4341 |
ond part matches one or more characters that are not parentheses. The |
ond part matches one or more characters that are not parentheses. The |
| 4342 |
third part is a conditional subpattern that tests whether the first set |
third part is a conditional subpattern that tests whether the first set |
| 4343 |
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 |
| 4344 |
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- |
| 4345 |
tern is executed and a closing parenthesis is required. Otherwise, |
tern is executed and a closing parenthesis is required. Otherwise, |
| 4346 |
since no-pattern is not present, the subpattern matches nothing. In |
since no-pattern is not present, the subpattern matches nothing. In |
| 4347 |
other words, this pattern matches a sequence of non-parentheses, |
other words, this pattern matches a sequence of non-parentheses, |
| 4348 |
optionally enclosed in parentheses. |
optionally enclosed in parentheses. |
| 4349 |
|
|
| 4350 |
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 |
| 4351 |
relative reference: |
relative reference: |
| 4352 |
|
|
| 4353 |
...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
| 4354 |
|
|
| 4355 |
This makes the fragment independent of the parentheses in the larger |
This makes the fragment independent of the parentheses in the larger |
| 4356 |
pattern. |
pattern. |
| 4357 |
|
|
| 4358 |
Checking for a used subpattern by name |
Checking for a used subpattern by name |
| 4359 |
|
|
| 4360 |
Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a |
Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a |
| 4361 |
used subpattern by name. For compatibility with earlier versions of |
used subpattern by name. For compatibility with earlier versions of |
| 4362 |
PCRE, which had this facility before Perl, the syntax (?(name)...) is |
PCRE, which had this facility before Perl, the syntax (?(name)...) is |
| 4363 |
also recognized. However, there is a possible ambiguity with this syn- |
also recognized. However, there is a possible ambiguity with this syn- |
| 4364 |
tax, because subpattern names may consist entirely of digits. PCRE |
tax, because subpattern names may consist entirely of digits. PCRE |
| 4365 |
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 |
| 4366 |
consists entirely of digits, PCRE looks for a subpattern of that num- |
consists entirely of digits, PCRE looks for a subpattern of that num- |
| 4367 |
ber, which must be greater than zero. Using subpattern names that con- |
ber, which must be greater than zero. Using subpattern names that con- |
| 4368 |
sist entirely of digits is not recommended. |
sist entirely of digits is not recommended. |
| 4369 |
|
|
| 4370 |
Rewriting the above example to use a named subpattern gives this: |
Rewriting the above example to use a named subpattern gives this: |
| 4375 |
Checking for pattern recursion |
Checking for pattern recursion |
| 4376 |
|
|
| 4377 |
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 |
| 4378 |
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 |
| 4379 |
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- |
| 4380 |
sand follow the letter R, for example: |
sand follow the letter R, for example: |
| 4381 |
|
|
| 4382 |
(?(R3)...) or (?(R&name)...) |
(?(R3)...) or (?(R&name)...) |
| 4383 |
|
|
| 4384 |
the condition is true if the most recent recursion is into the subpat- |
the condition is true if the most recent recursion is into the subpat- |
| 4385 |
tern whose number or name is given. This condition does not check the |
tern whose number or name is given. This condition does not check the |
| 4386 |
entire recursion stack. |
entire recursion stack. |
| 4387 |
|
|
| 4388 |
At "top level", all these recursion test conditions are false. Recur- |
At "top level", all these recursion test conditions are false. Recur- |
| 4389 |
sive patterns are described below. |
sive patterns are described below. |
| 4390 |
|
|
| 4391 |
Defining subpatterns for use by reference only |
Defining subpatterns for use by reference only |
| 4392 |
|
|
| 4393 |
If the condition is the string (DEFINE), and there is no subpattern |
If the condition is the string (DEFINE), and there is no subpattern |
| 4394 |
with the name DEFINE, the condition is always false. In this case, |
with the name DEFINE, the condition is always false. In this case, |
| 4395 |
there may be only one alternative in the subpattern. It is always |
there may be only one alternative in the subpattern. It is always |
| 4396 |
skipped if control reaches this point in the pattern; the idea of |
skipped if control reaches this point in the pattern; the idea of |
| 4397 |
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- |
| 4398 |
erenced from elsewhere. (The use of "subroutines" is described below.) |
erenced from elsewhere. (The use of "subroutines" is described below.) |
| 4399 |
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 |
| 4400 |
this (ignore whitespace and line breaks): |
this (ignore whitespace and line breaks): |
| 4401 |
|
|
| 4402 |
(?(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) ) |
| 4403 |
\b (?&byte) (\.(?&byte)){3} \b |
\b (?&byte) (\.(?&byte)){3} \b |
| 4404 |
|
|
| 4405 |
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 |
| 4406 |
group named "byte" is defined. This matches an individual component of |
group named "byte" is defined. This matches an individual component of |
| 4407 |
an IPv4 address (a number less than 256). When matching takes place, |
an IPv4 address (a number less than 256). When matching takes place, |
| 4408 |
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 |
| 4409 |
condition. |
condition. |
| 4410 |
|
|
| 4411 |
The rest of the pattern uses references to the named group to match the |
The rest of the pattern uses references to the named group to match the |
| 4412 |
four dot-separated components of an IPv4 address, insisting on a word |
four dot-separated components of an IPv4 address, insisting on a word |
| 4413 |
boundary at each end. |
boundary at each end. |
| 4414 |
|
|
| 4415 |
Assertion conditions |
Assertion conditions |
| 4416 |
|
|
| 4417 |
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 |
| 4418 |
assertion. This may be a positive or negative lookahead or lookbehind |
assertion. This may be a positive or negative lookahead or lookbehind |
| 4419 |
assertion. Consider this pattern, again containing non-significant |
assertion. Consider this pattern, again containing non-significant |
| 4420 |
white space, and with the two alternatives on the second line: |
white space, and with the two alternatives on the second line: |
| 4421 |
|
|
| 4422 |
(?(?=[^a-z]*[a-z]) |
(?(?=[^a-z]*[a-z]) |
| 4423 |
\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} ) |
| 4424 |
|
|
| 4425 |
The condition is a positive lookahead assertion that matches an |
The condition is a positive lookahead assertion that matches an |
| 4426 |
optional sequence of non-letters followed by a letter. In other words, |
optional sequence of non-letters followed by a letter. In other words, |
| 4427 |
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 |
| 4428 |
letter is found, the subject is matched against the first alternative; |
letter is found, the subject is matched against the first alternative; |
| 4429 |
otherwise it is matched against the second. This pattern matches |
otherwise it is matched against the second. This pattern matches |
| 4430 |
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 |
| 4431 |
letters and dd are digits. |
letters and dd are digits. |
| 4432 |
|
|
| 4433 |
|
|
| 4434 |
COMMENTS |
COMMENTS |
| 4435 |
|
|
| 4436 |
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 |
| 4437 |
next closing parenthesis. Nested parentheses are not permitted. The |
next closing parenthesis. Nested parentheses are not permitted. The |
| 4438 |
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 |
| 4439 |
at all. |
at all. |
| 4440 |
|
|
| 4441 |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
| 4442 |
character class introduces a comment that continues to immediately |
character class introduces a comment that continues to immediately |
| 4443 |
after the next newline in the pattern. |
after the next newline in the pattern. |
| 4444 |
|
|
| 4445 |
|
|
| 4446 |
RECURSIVE PATTERNS |
RECURSIVE PATTERNS |
| 4447 |
|
|
| 4448 |
Consider the problem of matching a string in parentheses, allowing for |
Consider the problem of matching a string in parentheses, allowing for |
| 4449 |
unlimited nested parentheses. Without the use of recursion, the best |
unlimited nested parentheses. Without the use of recursion, the best |
| 4450 |
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 |
| 4451 |
depth of nesting. It is not possible to handle an arbitrary nesting |
depth of nesting. It is not possible to handle an arbitrary nesting |
| 4452 |
depth. |
depth. |
| 4453 |
|
|
| 4454 |
For some time, Perl has provided a facility that allows regular expres- |
For some time, Perl has provided a facility that allows regular expres- |
| 4455 |
sions to recurse (amongst other things). It does this by interpolating |
sions to recurse (amongst other things). It does this by interpolating |
| 4456 |
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 |
| 4457 |
expression itself. A Perl pattern using code interpolation to solve the |
expression itself. A Perl pattern using code interpolation to solve the |
| 4458 |
parentheses problem can be created like this: |
parentheses problem can be created like this: |
| 4459 |
|
|
| 4463 |
refers recursively to the pattern in which it appears. |
refers recursively to the pattern in which it appears. |
| 4464 |
|
|
| 4465 |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, |
| 4466 |
it supports special syntax for recursion of the entire pattern, and |
it supports special syntax for recursion of the entire pattern, and |
| 4467 |
also for individual subpattern recursion. After its introduction in |
also for individual subpattern recursion. After its introduction in |
| 4468 |
PCRE and Python, this kind of recursion was introduced into Perl at |
PCRE and Python, this kind of recursion was introduced into Perl at |
| 4469 |
release 5.10. |
release 5.10. |
| 4470 |
|
|
| 4471 |
A special item that consists of (? followed by a number greater than |
A special item that consists of (? followed by a number greater than |
| 4472 |
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 |
| 4473 |
the given number, provided that it occurs inside that subpattern. (If |
the given number, provided that it occurs inside that subpattern. (If |
| 4474 |
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- |
| 4475 |
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 |
| 4476 |
regular expression. |
regular expression. |
| 4477 |
|
|
| 4478 |
In PCRE (like Python, but unlike Perl), a recursive subpattern call is |
In PCRE (like Python, but unlike Perl), a recursive subpattern call is |
| 4479 |
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 |
| 4480 |
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 |
| 4481 |
alternatives and there is a subsequent matching failure. |
alternatives and there is a subsequent matching failure. |
| 4482 |
|
|
| 4483 |
This PCRE pattern solves the nested parentheses problem (assume the |
This PCRE pattern solves the nested parentheses problem (assume the |
| 4484 |
PCRE_EXTENDED option is set so that white space is ignored): |
PCRE_EXTENDED option is set so that white space is ignored): |
| 4485 |
|
|
| 4486 |
\( ( (?>[^()]+) | (?R) )* \) |
\( ( (?>[^()]+) | (?R) )* \) |
| 4487 |
|
|
| 4488 |
First it matches an opening parenthesis. Then it matches any number of |
First it matches an opening parenthesis. Then it matches any number of |
| 4489 |
substrings which can either be a sequence of non-parentheses, or a |
substrings which can either be a sequence of non-parentheses, or a |
| 4490 |
recursive match of the pattern itself (that is, a correctly parenthe- |
recursive match of the pattern itself (that is, a correctly parenthe- |
| 4491 |
sized substring). Finally there is a closing parenthesis. |
sized substring). Finally there is a closing parenthesis. |
| 4492 |
|
|
| 4493 |
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 |
| 4494 |
the entire pattern, so instead you could use this: |
the entire pattern, so instead you could use this: |
| 4495 |
|
|
| 4496 |
( \( ( (?>[^()]+) | (?1) )* \) ) |
( \( ( (?>[^()]+) | (?1) )* \) ) |
| 4497 |
|
|
| 4498 |
We have put the pattern into parentheses, and caused the recursion to |
We have put the pattern into parentheses, and caused the recursion to |
| 4499 |
refer to them instead of the whole pattern. |
refer to them instead of the whole pattern. |
| 4500 |
|
|
| 4501 |
In a larger pattern, keeping track of parenthesis numbers can be |
In a larger pattern, keeping track of parenthesis numbers can be |
| 4502 |
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 |
| 4503 |
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 |
| 4504 |
(?-2) to refer to the second most recently opened parentheses preceding |
(?-2) to refer to the second most recently opened parentheses preceding |
| 4505 |
the recursion. In other words, a negative number counts capturing |
the recursion. In other words, a negative number counts capturing |
| 4506 |
parentheses leftwards from the point at which it is encountered. |
parentheses leftwards from the point at which it is encountered. |
| 4507 |
|
|
| 4508 |
It is also possible to refer to subsequently opened parentheses, by |
It is also possible to refer to subsequently opened parentheses, by |
| 4509 |
writing references such as (?+2). However, these cannot be recursive |
writing references such as (?+2). However, these cannot be recursive |
| 4510 |
because the reference is not inside the parentheses that are refer- |
because the reference is not inside the parentheses that are refer- |
| 4511 |
enced. They are always "subroutine" calls, as described in the next |
enced. They are always "subroutine" calls, as described in the next |
| 4512 |
section. |
section. |
| 4513 |
|
|
| 4514 |
An alternative approach is to use named parentheses instead. The Perl |
An alternative approach is to use named parentheses instead. The Perl |
| 4515 |
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 |
| 4516 |
supported. We could rewrite the above example as follows: |
supported. We could rewrite the above example as follows: |
| 4517 |
|
|
| 4518 |
(?<pn> \( ( (?>[^()]+) | (?&pn) )* \) ) |
(?<pn> \( ( (?>[^()]+) | (?&pn) )* \) ) |
| 4519 |
|
|
| 4520 |
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 |
| 4521 |
one is used. |
one is used. |
| 4522 |
|
|
| 4523 |
This particular example pattern that we have been looking at contains |
This particular example pattern that we have been looking at contains |
| 4524 |
nested unlimited repeats, and so the use of atomic grouping for match- |
nested unlimited repeats, and so the use of atomic grouping for match- |
| 4525 |
ing strings of non-parentheses is important when applying the pattern |
ing strings of non-parentheses is important when applying the pattern |
| 4526 |
to strings that do not match. For example, when this pattern is applied |
to strings that do not match. For example, when this pattern is applied |
| 4527 |
to |
to |
| 4528 |
|
|
| 4529 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 4530 |
|
|
| 4531 |
it yields "no match" quickly. However, if atomic grouping is not used, |
it yields "no match" quickly. However, if atomic grouping is not used, |
| 4532 |
the match runs for a very long time indeed because there are so many |
the match runs for a very long time indeed because there are so many |
| 4533 |
different ways the + and * repeats can carve up the subject, and all |
different ways the + and * repeats can carve up the subject, and all |
| 4534 |
have to be tested before failure can be reported. |
have to be tested before failure can be reported. |
| 4535 |
|
|
| 4536 |
At the end of a match, the values set for any capturing subpatterns are |
At the end of a match, the values set for any capturing subpatterns are |
| 4537 |
those from the outermost level of the recursion at which the subpattern |
those from the outermost level of the recursion at which the subpattern |
| 4538 |
value is set. If you want to obtain intermediate values, a callout |
value is set. If you want to obtain intermediate values, a callout |
| 4539 |
function can be used (see below and the pcrecallout documentation). If |
function can be used (see below and the pcrecallout documentation). If |
| 4540 |
the pattern above is matched against |
the pattern above is matched against |
| 4541 |
|
|
| 4542 |
(ab(cd)ef) |
(ab(cd)ef) |
| 4543 |
|
|
| 4544 |
the value for the capturing parentheses is "ef", which is the last |
the value for the capturing parentheses is "ef", which is the last |
| 4545 |
value taken on at the top level. If additional parentheses are added, |
value taken on at the top level. If additional parentheses are added, |
| 4546 |
giving |
giving |
| 4547 |
|
|
| 4548 |
\( ( ( (?>[^()]+) | (?R) )* ) \) |
\( ( ( (?>[^()]+) | (?R) )* ) \) |
| 4549 |
^ ^ |
^ ^ |
| 4550 |
^ ^ |
^ ^ |
| 4551 |
|
|
| 4552 |
the string they capture is "ab(cd)ef", the contents of the top level |
the string they capture is "ab(cd)ef", the contents of the top level |
| 4553 |
parentheses. If there are more than 15 capturing parentheses in a pat- |
parentheses. If there are more than 15 capturing parentheses in a pat- |
| 4554 |
tern, PCRE has to obtain extra memory to store data during a recursion, |
tern, PCRE has to obtain extra memory to store data during a recursion, |
| 4555 |
which it does by using pcre_malloc, freeing it via pcre_free after- |
which it does by using pcre_malloc, freeing it via pcre_free after- |
| 4556 |
wards. If no memory can be obtained, the match fails with the |
wards. If no memory can be obtained, the match fails with the |
| 4557 |
PCRE_ERROR_NOMEMORY error. |
PCRE_ERROR_NOMEMORY error. |
| 4558 |
|
|
| 4559 |
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 |
| 4560 |
recursion. Consider this pattern, which matches text in angle brack- |
recursion. Consider this pattern, which matches text in angle brack- |
| 4561 |
ets, allowing for arbitrary nesting. Only digits are allowed in nested |
ets, allowing for arbitrary nesting. Only digits are allowed in nested |
| 4562 |
brackets (that is, when recursing), whereas any characters are permit- |
brackets (that is, when recursing), whereas any characters are permit- |
| 4563 |
ted at the outer level. |
ted at the outer level. |
| 4564 |
|
|
| 4565 |
< (?: (?(R) \d++ | [^<>]*+) | (?R)) * > |
< (?: (?(R) \d++ | [^<>]*+) | (?R)) * > |
| 4566 |
|
|
| 4567 |
In this pattern, (?(R) is the start of a conditional subpattern, with |
In this pattern, (?(R) is the start of a conditional subpattern, with |
| 4568 |
two different alternatives for the recursive and non-recursive cases. |
two different alternatives for the recursive and non-recursive cases. |
| 4569 |
The (?R) item is the actual recursive call. |
The (?R) item is the actual recursive call. |
| 4570 |
|
|
| 4571 |
|
|
| 4572 |
SUBPATTERNS AS SUBROUTINES |
SUBPATTERNS AS SUBROUTINES |
| 4573 |
|
|
| 4574 |
If the syntax for a recursive subpattern reference (either by number or |
If the syntax for a recursive subpattern reference (either by number or |
| 4575 |
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- |
| 4576 |
ates like a subroutine in a programming language. The "called" subpat- |
ates like a subroutine in a programming language. The "called" subpat- |
| 4577 |
tern may be defined before or after the reference. A numbered reference |
tern may be defined before or after the reference. A numbered reference |
| 4578 |
can be absolute or relative, as in these examples: |
can be absolute or relative, as in these examples: |
| 4579 |
|
|
| 4585 |
|
|
| 4586 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
| 4587 |
|
|
| 4588 |
matches "sense and sensibility" and "response and responsibility", but |
matches "sense and sensibility" and "response and responsibility", but |
| 4589 |
not "sense and responsibility". If instead the pattern |
not "sense and responsibility". If instead the pattern |
| 4590 |
|
|
| 4591 |
(sens|respons)e and (?1)ibility |
(sens|respons)e and (?1)ibility |
| 4592 |
|
|
| 4593 |
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 |
| 4594 |
two strings. Another example is given in the discussion of DEFINE |
two strings. Another example is given in the discussion of DEFINE |
| 4595 |
above. |
above. |
| 4596 |
|
|
| 4597 |
Like recursive subpatterns, a "subroutine" call is always treated as an |
Like recursive subpatterns, a "subroutine" call is always treated as an |
| 4598 |
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, |
| 4599 |
it is never re-entered, even if it contains untried alternatives and |
it is never re-entered, even if it contains untried alternatives and |
| 4600 |
there is a subsequent matching failure. |
there is a subsequent matching failure. |
| 4601 |
|
|
| 4602 |
When a subpattern is used as a subroutine, processing options such as |
When a subpattern is used as a subroutine, processing options such as |
| 4603 |
case-independence are fixed when the subpattern is defined. They cannot |
case-independence are fixed when the subpattern is defined. They cannot |
| 4604 |
be changed for different calls. For example, consider this pattern: |
be changed for different calls. For example, consider this pattern: |
| 4605 |
|
|
| 4606 |
(abc)(?i:(?-1)) |
(abc)(?i:(?-1)) |
| 4607 |
|
|
| 4608 |
It matches "abcabc". It does not match "abcABC" because the change of |
It matches "abcabc". It does not match "abcABC" because the change of |
| 4609 |
processing option does not affect the called subpattern. |
processing option does not affect the called subpattern. |
| 4610 |
|
|
| 4611 |
|
|
| 4612 |
CALLOUTS |
CALLOUTS |
| 4613 |
|
|
| 4614 |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary |
| 4615 |
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. |
| 4616 |
This makes it possible, amongst other things, to extract different sub- |
This makes it possible, amongst other things, to extract different sub- |
| 4617 |
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- |
| 4618 |
tion. |
tion. |
| 4619 |
|
|
| 4620 |
PCRE provides a similar feature, but of course it cannot obey arbitrary |
PCRE provides a similar feature, but of course it cannot obey arbitrary |
| 4621 |
Perl code. The feature is called "callout". The caller of PCRE provides |
Perl code. The feature is called "callout". The caller of PCRE provides |
| 4622 |
an external function by putting its entry point in the global variable |
an external function by putting its entry point in the global variable |
| 4623 |
pcre_callout. By default, this variable contains NULL, which disables |
pcre_callout. By default, this variable contains NULL, which disables |
| 4624 |
all calling out. |
all calling out. |
| 4625 |
|
|
| 4626 |
Within a regular expression, (?C) indicates the points at which the |
Within a regular expression, (?C) indicates the points at which the |
| 4627 |
external function is to be called. If you want to identify different |
external function is to be called. If you want to identify different |
| 4628 |
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. |
| 4629 |
The default value is zero. For example, this pattern has two callout |
The default value is zero. For example, this pattern has two callout |
| 4630 |
points: |
points: |
| 4631 |
|
|
| 4632 |
(?C1)abc(?C2)def |
(?C1)abc(?C2)def |
| 4633 |
|
|
| 4634 |
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 |
| 4635 |
automatically installed before each item in the pattern. They are all |
automatically installed before each item in the pattern. They are all |
| 4636 |
numbered 255. |
numbered 255. |
| 4637 |
|
|
| 4638 |
During matching, when PCRE reaches a callout point (and pcre_callout is |
During matching, when PCRE reaches a callout point (and pcre_callout is |
| 4639 |
set), the external function is called. It is provided with the number |
set), the external function is called. It is provided with the number |
| 4640 |
of the callout, the position in the pattern, and, optionally, one item |
of the callout, the position in the pattern, and, optionally, one item |
| 4641 |
of data originally supplied by the caller of pcre_exec(). The callout |
of data originally supplied by the caller of pcre_exec(). The callout |
| 4642 |
function may cause matching to proceed, to backtrack, or to fail alto- |
function may cause matching to proceed, to backtrack, or to fail alto- |
| 4643 |
gether. A complete description of the interface to the callout function |
gether. A complete description of the interface to the callout function |
| 4644 |
is given in the pcrecallout documentation. |
is given in the pcrecallout documentation. |
| 4645 |
|
|
| 4646 |
|
|
| 4647 |
BACTRACKING CONTROL |
BACTRACKING CONTROL |
| 4648 |
|
|
| 4649 |
Perl 5.10 introduced a number of "Special Backtracking Control Verbs", |
Perl 5.10 introduced a number of "Special Backtracking Control Verbs", |
| 4650 |
which are described in the Perl documentation as "experimental and sub- |
which are described in the Perl documentation as "experimental and sub- |
| 4651 |
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 |
| 4652 |
say: "Their usage in production code should be noted to avoid problems |
say: "Their usage in production code should be noted to avoid problems |
| 4653 |
during upgrades." The same remarks apply to the PCRE features described |
during upgrades." The same remarks apply to the PCRE features described |
| 4654 |
in this section. |
in this section. |
| 4655 |
|
|
| 4656 |
Since these verbs are specifically related to backtracking, they can be |
Since these verbs are specifically related to backtracking, they can be |
| 4657 |
used only when the pattern is to be matched using pcre_exec(), which |
used only when the pattern is to be matched using pcre_exec(), which |
| 4658 |
uses a backtracking algorithm. They cause an error if encountered by |
uses a backtracking algorithm. They cause an error if encountered by |
| 4659 |
pcre_dfa_exec(). |
pcre_dfa_exec(). |
| 4660 |
|
|
| 4661 |
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- |
| 4662 |
ing parenthesis followed by an asterisk. In Perl, they are generally of |
ing parenthesis followed by an asterisk. In Perl, they are generally of |
| 4663 |
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 |
| 4664 |
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 |
| 4665 |
in a pattern. There are two kinds: |
in a pattern. There are two kinds: |
| 4666 |
|
|
| 4667 |
Verbs that act immediately |
Verbs that act immediately |
| 4670 |
|
|
| 4671 |
(*ACCEPT) |
(*ACCEPT) |
| 4672 |
|
|
| 4673 |
This verb causes the match to end successfully, skipping the remainder |
This verb causes the match to end successfully, skipping the remainder |
| 4674 |
of the pattern. When inside a recursion, only the innermost pattern is |
of the pattern. When inside a recursion, only the innermost pattern is |
| 4675 |
ended immediately. PCRE differs from Perl in what happens if the |
ended immediately. PCRE differs from Perl in what happens if the |
| 4676 |
(*ACCEPT) is inside capturing parentheses. In Perl, the data so far is |
(*ACCEPT) is inside capturing parentheses. In Perl, the data so far is |
| 4677 |
captured: in PCRE no data is captured. For example: |
captured: in PCRE no data is captured. For example: |
| 4678 |
|
|
| 4679 |
A(A|B(*ACCEPT)|C)D |
A(A|B(*ACCEPT)|C)D |
| 4680 |
|
|
| 4681 |
This matches "AB", "AAD", or "ACD", but when it matches "AB", no data |
This matches "AB", "AAD", or "ACD", but when it matches "AB", no data |
| 4682 |
is captured. |
is captured. |
| 4683 |
|
|
| 4684 |
(*FAIL) or (*F) |
(*FAIL) or (*F) |
| 4685 |
|
|
| 4686 |
This verb causes the match to fail, forcing backtracking to occur. It |
This verb causes the match to fail, forcing backtracking to occur. It |
| 4687 |
is equivalent to (?!) but easier to read. The Perl documentation notes |
is equivalent to (?!) but easier to read. The Perl documentation notes |
| 4688 |
that it is probably useful only when combined with (?{}) or (??{}). |
that it is probably useful only when combined with (?{}) or (??{}). |
| 4689 |
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 |
| 4690 |
nearest equivalent is the callout feature, as for example in this pat- |
nearest equivalent is the callout feature, as for example in this pat- |
| 4691 |
tern: |
tern: |
| 4692 |
|
|
| 4693 |
a+(?C)(*FAIL) |
a+(?C)(*FAIL) |
| 4694 |
|
|
| 4695 |
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 |
| 4696 |
before each backtrack happens (in this example, 10 times). |
before each backtrack happens (in this example, 10 times). |
| 4697 |
|
|
| 4698 |
Verbs that act after backtracking |
Verbs that act after backtracking |
| 4699 |
|
|
| 4700 |
The following verbs do nothing when they are encountered. Matching con- |
The following verbs do nothing when they are encountered. Matching con- |
| 4701 |
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- |
| 4702 |
ure is forced. The verbs differ in exactly what kind of failure |
ure is forced. The verbs differ in exactly what kind of failure |
| 4703 |
occurs. |
occurs. |
| 4704 |
|
|
| 4705 |
(*COMMIT) |
(*COMMIT) |
| 4706 |
|
|
| 4707 |
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 |
| 4708 |
pattern does not match. Even if the pattern is unanchored, no further |
pattern does not match. Even if the pattern is unanchored, no further |
| 4709 |
attempts to find a match by advancing the start point take place. Once |
attempts to find a match by advancing the start point take place. Once |
| 4710 |
(*COMMIT) has been passed, pcre_exec() is committed to finding a match |
(*COMMIT) has been passed, pcre_exec() is committed to finding a match |
| 4711 |
at the current starting point, or not at all. For example: |
at the current starting point, or not at all. For example: |
| 4712 |
|
|
| 4713 |
a+(*COMMIT)b |
a+(*COMMIT)b |
| 4714 |
|
|
| 4715 |
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 |
| 4716 |
of dynamic anchor, or "I've started, so I must finish." |
of dynamic anchor, or "I've started, so I must finish." |
| 4717 |
|
|
| 4718 |
(*PRUNE) |
(*PRUNE) |
| 4719 |
|
|
| 4720 |
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 |
| 4721 |
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 |
| 4722 |
"bumpalong" advance to the next starting character then happens. Back- |
"bumpalong" advance to the next starting character then happens. Back- |
| 4723 |
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 |
| 4724 |
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- |
| 4725 |
tracking cannot cross (*PRUNE). In simple cases, the use of (*PRUNE) |
tracking cannot cross (*PRUNE). In simple cases, the use of (*PRUNE) |
| 4726 |
is just an alternative to an atomic group or possessive quantifier, but |
is just an alternative to an atomic group or possessive quantifier, but |
| 4727 |
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 |
| 4728 |
way. |
way. |
| 4729 |
|
|
| 4730 |
(*SKIP) |
(*SKIP) |
| 4731 |
|
|
| 4732 |
This verb is like (*PRUNE), except that if the pattern is unanchored, |
This verb is like (*PRUNE), except that if the pattern is unanchored, |
| 4733 |
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- |
| 4734 |
tion in the subject where (*SKIP) was encountered. (*SKIP) signifies |
tion in the subject where (*SKIP) was encountered. (*SKIP) signifies |
| 4735 |
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 |
| 4736 |
successful match. Consider: |
successful match. Consider: |
| 4737 |
|
|
| 4738 |
a+(*SKIP)b |
a+(*SKIP)b |
| 4739 |
|
|
| 4740 |
If the subject is "aaaac...", after the first match attempt fails |
If the subject is "aaaac...", after the first match attempt fails |
| 4741 |
(starting at the first character in the string), the starting point |
(starting at the first character in the string), the starting point |
| 4742 |
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- |
| 4743 |
tifer does not have the same effect in this example; although it would |
tifer does not have the same effect in this example; although it would |
| 4744 |
suppress backtracking during the first match attempt, the second |
suppress backtracking during the first match attempt, the second |
| 4745 |
attempt would start at the second character instead of skipping on to |
attempt would start at the second character instead of skipping on to |
| 4746 |
"c". |
"c". |
| 4747 |
|
|
| 4748 |
(*THEN) |
(*THEN) |
| 4749 |
|
|
| 4750 |
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- |
| 4751 |
tern does not match. That is, it cancels pending backtracking, but only |
tern does not match. That is, it cancels pending backtracking, but only |
| 4752 |
within the current alternation. Its name comes from the observation |
within the current alternation. Its name comes from the observation |
| 4753 |
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: |
| 4754 |
|
|
| 4755 |
( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... |
( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... |
| 4756 |
|
|
| 4757 |
If the COND1 pattern matches, FOO is tried (and possibly further items |
If the COND1 pattern matches, FOO is tried (and possibly further items |
| 4758 |
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 |
| 4759 |
skips to the second alternative and tries COND2, without backtracking |
skips to the second alternative and tries COND2, without backtracking |
| 4760 |
into COND1. If (*THEN) is used outside of any alternation, it acts |
into COND1. If (*THEN) is used outside of any alternation, it acts |
| 4761 |
exactly like (*PRUNE). |
exactly like (*PRUNE). |
| 4762 |
|
|
| 4763 |
|
|
| 4775 |
|
|
| 4776 |
REVISION |
REVISION |
| 4777 |
|
|
| 4778 |
Last updated: 09 August 2007 |
Last updated: 21 August 2007 |
| 4779 |
Copyright (c) 1997-2007 University of Cambridge. |
Copyright (c) 1997-2007 University of Cambridge. |
| 4780 |
------------------------------------------------------------------------------ |
------------------------------------------------------------------------------ |
| 4781 |
|
|
| 5072 |
(*THEN) local failure, backtrack to next alternation |
(*THEN) local failure, backtrack to next alternation |
| 5073 |
|
|
| 5074 |
|
|
| 5075 |
|
NEWLINE CONVENTIONS |
| 5076 |
|
|
| 5077 |
|
These are recognized only at the very start of a pattern. |
| 5078 |
|
|
| 5079 |
|
(*CR) |
| 5080 |
|
(*LF) |
| 5081 |
|
(*CRLF) |
| 5082 |
|
(*ANYCRLF) |
| 5083 |
|
(*ANY) |
| 5084 |
|
|
| 5085 |
|
|
| 5086 |
CALLOUTS |
CALLOUTS |
| 5087 |
|
|
| 5088 |
(?C) callout |
(?C) callout |
| 5103 |
|
|
| 5104 |
REVISION |
REVISION |
| 5105 |
|
|
| 5106 |
Last updated: 08 August 2007 |
Last updated: 21 August 2007 |
| 5107 |
Copyright (c) 1997-2007 University of Cambridge. |
Copyright (c) 1997-2007 University of Cambridge. |
| 5108 |
------------------------------------------------------------------------------ |
------------------------------------------------------------------------------ |
| 5109 |
|
|