develooper Front page | perl.perl5.porters | Postings from December 2016

[perl #3589] Regexp behavior in matching backreferences to optionalcaptures

Thread Previous | Thread Next
James E Keenan via RT
December 28, 2016 02:16
[perl #3589] Regexp behavior in matching backreferences to optionalcaptures
Message ID:
On Sun, 25 Jul 2010 19:21:33 GMT, greerga wrote:
> The remaining part of this ticket is clarifying in the documentation the
> behavior of back-references to optional captures.  For example:
> ./perl -Ilib -wle '/^((.)?a\2)+$/ and print "$_: -$1-$2-" for qw/
> babadad /' 
> does not match, because the "\2" always fails (rather than allowing an
> empty match) if the "." didn't match.  However:
> ./perl -Ilib -wle '/^((.?)a\2)+$/ and print "$_: -$1-$2-" for qw/
> babadad /' 
> does match, because an empty result is valid in the capture.

I believe this ticket is closable because the additional documentation needed was, in my reading, added by Karl Williamson in commit d8b950dc in June 2010.  'pod/perlre.pod' has a section on Capture Groups which contains this:

The bracketing construct "( ... )" creates capture groups
(also referred to as capture buffers). To refer to the
current contents of a group later on, within the same
pattern, use "\g1" (or "\g{1}") for the first, "\g2" (or
"\g{2}") for the second, and so on. This is called a
*backreference*. There is no limit to the number of captured
substrings that you may use. Groups are numbered with the
leftmost open parenthesis being number 1, etc. If a group
did not match, the associated backreference won't match
either. (This can happen if the group is optional, or in a
different branch of an alternation.)

I'll put this ticket out of its misery within 7 days unless someone wants to carry on the discussion.

Thank you very much.

James E Keenan (

via perlbug:  queue: perl5 status: open

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About