diff --git a/reference/pcre/pattern.syntax.xml b/reference/pcre/pattern.syntax.xml index b0092132d2..83481aa2ae 100644 --- a/reference/pcre/pattern.syntax.xml +++ b/reference/pcre/pattern.syntax.xml @@ -1282,7 +1282,8 @@ As of PHP 4.3.3, it is possible to name a subpattern using the syntax (?P<name>pattern). This subpattern will then be indexed in the matches array by its normal numeric position and - also by name. + also by name. PHP 5.2.2 introduced two alternative syntaxes + (?<name>pattern) and (?'name'pattern). @@ -1537,12 +1538,32 @@ reference. This can be done using alternation, as in the example above, or by a quantifier with a minimum of zero. - + + As of PHP 5.2.2, the \g escape sequence can be + used for absolute and relative referencing of subpatterns. + This escape sequence must be followed by an unsigned number or a negative + number, optionally enclosed in braces. The sequences \1, + \g1 and \g{1} are synonymous + with one another. The use of this pattern with an unsigned number can + help remove the ambiguity inherent when using digits following a + backslash. The sequence helps to distinguish back references from octal + characters and also makes it easier to have a back reference followed + by a literal number, e.g. \g{2}1. + + + The use of the \g sequence with a negative number + signifies a relative reference. For example, (foo)(bar)\g{-1} + would match the sequence "foobarbar" and (foo)(bar)\g{-2} + matches "foobarfoo". This can be useful in long patterns as an alternative + to keeping track of the number of subpatterns in order to reference + a specific previous subpattern. + Back references to the named subpatterns can be achieved by - (?P=name) or, since PHP 5.2.4, also by - \k<name>, \k'name', - \k{name} or \g{name}. + (?P=name) or, since PHP 5.2.2, also by + \k<name> or \k'name'. + Additionally PHP 5.2.4 added support for \k{name} + and \g{name}.