-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regexp behavior in matching backreferences to optional captures #2271
Comments
From @vanstynCreated by @vanstyncrypt% perl -wle '/^((.)?a\2)+$/ and print "$_: -$1-$2-" for qw/ babadad babadab bababdab bababdad /' That's with 5.6.0; with bleadperl, none of the strings match. Perl Info
|
From @vanstynMore: That odd number turns out to occur because PL_reg_start_tmp[paren] Hugo |
From @smpeters
It appears that there is a problem with your regexp. /^((.)?a\2)+$/ The "^" is saying start at the beginning of the string, and none of the steve@kirk sandbox $ perl -wle '/^((.)?a\2)+$/ and print "$_: -$1-$2-" "dad" matches exactly as does "daddad". "dadad" does not match since This problem does not appear to be a bug with Perl, but with the regular |
@smpeters - Status changed from 'open' to 'rejected' |
From @hvds"Steve Peters via RT" <perlbug-followup@perl.org> wrote: That depends on your definitions, and I think the problem here is that If the '?' is moved inside the parens: As written though, it isn't clear what the following '\2' should match Behaviour from 5.6.1 onwards appears to take the last option: when It is however slightly unfortunate, in that the alternative definition Such forms can always be modified to give the desire behaviour though, Hugo |
@smpeters - Status changed from 'rejected' to 'open' |
From @ysthOn Sat, Sep 18, 2004 at 12:16:05AM +0100, hv@crypt.org wrote:
I second the reasonableness of this behaviour. |
From @smpetersOn Sunday 19 September 2004 02:18 pm, Yitzchak Scott-Thoennes wrote:
With all this being said, is it agreed that the current behavior is correct, Steve Peters |
From @hvdsSteve Peters <steve@fisharerojo.org> wrote: I think so, but I haven't actually checked the documentation and existing (I've also only just noticed that the original bug report came from me, I note that my subsequent comment is still true with latest bleadperl: zen% ./perl -Ilib -Dr -wle '/^((.)?a\2)+$/ and print "$_: -$1-$2-" for qw/ babadad /' 2>&1 | grep restoring .. and I assume my analysis from then is also still true: :That odd number turns out to occur because PL_reg_start_tmp[paren] Hugo |
From @rurbanpl-bug3589.patchdifforig perl-current/t/op/re_tests
2007-07-02 Reini Urban <rurban@x-ray.at>
* t/op/re_tests: test correct new behaviour for bug #3589 to close it.
Todo: documentation. See http://rt.perl.org/rt3/Public/Bug/Display.html?id=3589
diff -ub perl-current/t/op/re_tests.orig perl-current/t/op/re_tests
--- perl-current/t/op/re_tests.orig 2007-06-18 15:14:31.000000000 +0000
+++ perl-current/t/op/re_tests 2007-07-02 21:50:56.430125000 +0000
@@ -261,6 +261,8 @@
((\3|b)\2(a)x)+ aaxabxbaxbbx n - -
((\3|b)\2(a)x)+ aaaxabaxbaaxbbax y $&-$1-$2-$3 bbax-bbax-b-a
((\3|b)\2(a)){2,} bbaababbabaaaaabbaaaabba y $&-$1-$2-$3 bbaaaabba-bba-b-a
+#Bug #3589 - up to perl-5.6.0 matches incorrectly, from 5.6.1 not anymore
+^((.)?a\2)+$ babadad n - -
(a)|(b) b y $-[0] 0
(a)|(b) b y $+[0] 1
(a)|(b) b y x$-[1] x
|
From @rgsOn 02/07/07, Reini Urban via RT <perlbug-followup@perl.org> wrote:
Thanks, applied as #31530. |
From module@renee-baecker.deOn Mi. 04. Jul. 2007, 06:39:37, rafael wrote:
Attached is a patch for perlre.pod. If this patch is ok, this ticket can |
From module@renee-baecker.deperlre_pod.patch--- old/perl-5.10.0/pod/perlre.pod 2007-12-18 11:47:08.000000000 +0100
+++ perl-5.10.0/pod/perlre.pod 2008-05-27 10:33:47.000000000 +0200
@@ -599,6 +599,11 @@
which makes it easier to write code that tests for a series of more
specific cases and remembers the best match.
+B<NOTE>: If an optional match (e.g. C<(.)?>) does not match, the
+subsequent attempt to match the correspondent capture buffer fails.
+
+ 'bababdad' =~ /^((.)?a\2)+$/
+
B<WARNING>: Once Perl sees that you need one of C<$&>, C<$`>, or
C<$'> anywhere in the program, it has to provide them for every
pattern match. This may substantially slow your program. Perl
|
From p5p@perl.wizbit.beQuoting reneeb via RT <perlbug-followup@perl.org>:
I feel that the example should be clearer or it needs more explenation... From the example I can't immediatly tell what is happening and/or Kind regards, Bram |
From @gannett-ggreerThe remaining part of this ticket is clarifying in the documentation the ./perl -Ilib -wle '/^((.)?a\2)+$/ and print "$_: -$1-$2-" for qw/ does not match, because the "\2" always fails (rather than allowing an ./perl -Ilib -wle '/^((.?)a\2)+$/ and print "$_: -$1-$2-" for qw/ does match, because an empty result is valid in the capture. -- |
From @jkeenanOn Sun, 25 Jul 2010 19:21:33 GMT, greerga wrote:
I believe this ticket is closable because the additional documentation needed was, in my reading, added by Karl Williamson in commit d8b950d in June 2010. 'pod/perlre.pod' has a section on Capture Groups which contains this: ##### I'll put this ticket out of its misery within 7 days unless someone wants to carry on the discussion. Thank you very much. -- |
From @demerphqOn 28 December 2016 at 03:15, James E Keenan via RT
I think that should be beefed up a bit to say something like Note that a quantified group which matches zero things counts as a "aba"= However when the quantifier is on the inside of the group it will: "aba"= Put another way, perl's regex engine distinguishes between "captured You can see this in the following code: $ perl -le'if ("aba"=~/a(x*)b/) { print "matched: ", defined($1) ? $ perl -le'if ("aba"=~/a(x)*b/) { print "matched: ", defined($1) ? Both patterns match, but leave the contents of the $1 capture group in Yves |
Migrated from rt.perl.org#3589 (status was 'open')
Searchable as RT3589$
The text was updated successfully, but these errors were encountered: