left parentheses have opened before it. Likewise \11 is a
backreference only if at least 11 left parentheses have opened
before it. And so on. \1 through \9 are always interpreted as
-backreferences.
+backreferences.
X<relative backreference>
In Perl 5.10 it is possible to relatively address a capture buffer by
using the C<\RNNN> notation, where C<NNN> is negative offset to a
-preceding completed capture buffer. Thus C<\R1> refers to the last
-buffer closed, C<\R2> refers to the buffer before that, and so on. Note
-especially that C</(foo)(\R1)/> refers to the capture buffer containing
-C<foo>, not to the buffer containing C<\R1>.
+preceding capture buffer. Thus C<\R1> refers to the last buffer,
+C<\R2> refers to the buffer before that. For example:
+
+ /
+ (Y) # buffer 1
+ ( # buffer 2
+ (X) # buffer 3
+ \R1 # backref to buffer 3
+ \R3 # backref to buffer 1
+ )
+ /x
+
+and would match the same as C</(Y) ( (X) $3 $1 )/x>.
Additionally, as of Perl 5.10 you may use named capture buffers and named
backreferences. The notation is C<< (?<name>...) >> and C<< \k<name> >>
#define RExC_seen (pRExC_state->seen)
#define RExC_size (pRExC_state->size)
#define RExC_npar (pRExC_state->npar)
-#define RExC_cpar (pRExC_state->cpar)
#define RExC_nestroot (pRExC_state->nestroot)
#define RExC_extralen (pRExC_state->extralen)
#define RExC_seen_zerolen (pRExC_state->seen_zerolen)
RExC_end = xend;
RExC_naughty = 0;
RExC_npar = 1;
- RExC_cpar = 1;
RExC_nestroot = 0;
RExC_size = 0L;
RExC_emit = &PL_regdummy;
RExC_end = xend;
RExC_naughty = 0;
RExC_npar = 1;
- RExC_cpar = 1;
RExC_emit_start = ri->program;
RExC_emit = ri->program;
#ifdef DEBUGGING
ender = reg_node(pRExC_state, TAIL);
break;
case 1:
- RExC_cpar++;
ender = reganode(pRExC_state, CLOSE, parno);
if (!SIZE_ONLY && RExC_seen & REG_SEEN_RECURSE) {
DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
RExC_parse++;
num = atoi(RExC_parse);
if (isrel) {
- num = RExC_cpar - num;
+ num = RExC_npar - num;
if (num < 1)
vFAIL("Reference to nonexistent or unclosed group");
}
if (!SIZE_ONLY) {
if (num > (I32)RExC_rx->nparens)
vFAIL("Reference to nonexistent group");
- /* People make this error all the time apparently.
- So we cant fail on it, even though we should
-
- else if (num >= RExC_cpar)
- vFAIL("Reference to unclosed group will always match");
- */
}
RExC_sawback = 1;
ret = reganode(pRExC_state,
$v='foo';
iseq("$1",'bar','$1 is safe after /g - may fail due to specialized config in pp_hot.c')
}
+{
+ local $Message = "http://nntp.perl.org/group/perl.perl5.porters/118663";
+ my $qr_barR1 = qr/(bar)\R1/;
+ ok("foobarbarxyz" =~ $qr_barR1);
+ ok("foobarbarxyz" =~ qr/foo${qr_barR1}xyz/);
+ ok("foobarbarxyz" =~ qr/(foo)${qr_barR1}xyz/);
+ ok("foobarbarxyz" =~ qr/(foo)(bar)\R1xyz/);
+ ok("foobarbarxyz" =~ qr/(foo${qr_barR1})xyz/);
+ ok("foobarbarxyz" =~ qr/(foo(bar)\R1)xyz/);
+}
# Test counter is at bottom of file. Put new tests above here.
#-------------------------------------------------------------------
iseq(0+$::test,$::TestCount,"Got the right number of tests!");
# Don't forget to update this!
BEGIN {
- $::TestCount = 1369;
+ $::TestCount = 1375;
print "1..$::TestCount\n";
}
(a)(?:(?-1)|(?+1))(b) abb y $1-$2 a-b
(a)(?:(?-1)|(?+1))(b) acb n - -
-(foo)(\R1) foofoo y $1-$2 foo-foo
-(foo)(\R1)(foo)(\R1) foofoofoofoo y $1-$2-$3-$4 foo-foo-foo-foo
+(foo)(\R2) foofoo y $1-$2 foo-foo
+(foo)(\R2)(foo)(\R2) foofoofoofoo y $1-$2-$3-$4 foo-foo-foo-foo
+(([abc]+) \R1)(([abc]+) \R1) abc abccba cba y $2-$4 abc-cba