From: Moritz Lenz Date: Thu, 26 Nov 2009 23:33:09 +0000 (+0100) Subject: Document backreferences to groups that did not match X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=408633379a1452b4e14d7c3b5e80f7dc05ea7986;p=p5sagit%2Fp5-mst-13.2.git Document backreferences to groups that did not match Also add a test for that, fill in test description, and sneak in a vim modeline for re_tests --- diff --git a/pod/perlre.pod b/pod/perlre.pod index df627ff..42017dd 100644 --- a/pod/perlre.pod +++ b/pod/perlre.pod @@ -518,6 +518,10 @@ backreference only if at least 11 left parentheses have opened before it. And so on. \1 through \9 are always interpreted as backreferences. +If the bracketing group did not match, the associated backreference won't +match either. (This can happen if the bracketing group is optional, or +in a different branch of an alternation.) + X<\g{1}> X<\g{-1}> X<\g{name}> X X In order to provide a safer and easier way to construct patterns using backreferences, Perl provides the C<\g{N}> notation (starting with perl diff --git a/t/re/re_tests b/t/re/re_tests index 725a752..dc03084 100644 --- a/t/re/re_tests +++ b/t/re/re_tests @@ -275,7 +275,8 @@ a[-]?c ac y $& ac \g{0} - c - Reference to invalid group 0 \g{-0} - c - Reference to invalid group 0 (a)|\1 a y - - -(a)|\1 x n - - +(a)|\1 x n - Reference to group in different branch +(?:(b)?a)\1 a n - Reference to group that did not match (a)|\2 - c - Reference to nonexistent group (([a-c])b*?\2)* ababbbcbc y $&-$1-$2 ababb-bb-b (([a-c])b*?\2){3} ababbbcbc y $&-$1-$2 ababbbcbc-cbc-c @@ -1393,3 +1394,5 @@ foo(\h)bar foo\tbar y $1 \t '[\x{100}\xff]'i \x{ff} y $& \x{ff} ((??{ "(?:|)" }))\s C\x20 y - - + +# vim: set noexpandtab