Home: Perl Programming Help: Regular Expressions:
Perl Regex: not processing back references correctly



rajkumar
New User

Oct 9, 2012, 11:05 PM


Views: 13455
Perl Regex: not processing back references correctly

Hi,

According to: http://www.boost.org/doc/libs/1_33_1/libs/regex/doc/syntax_perl.html

^(a*).*\1$

should not match

aaabba

But I have checked out many perl online testing site and perl script also, all matches above string correctly.

I am confused now , which one is correct 1. Boost documentation 2. Perl script.
Please let me know which one is correct.

I am not able to understand ^(a*).*\1$ regex also. Please assist me to understand this also.

Thanks in advance.


Laurent_R
Veteran / Moderator

Oct 10, 2012, 4:49 AM


Views: 13450
Re: [rajkumar] Perl Regex: not processing back references correctly

Hi,

I see no reason why ^(a*).*\1$ should fail to match aaabba.

And I confirm that it DOES indeed match it.

The regex engine first tries "aaa", but fails on the back reference. It then backtracks and tries "aa", and fails again. It backtracks again and tries "a", and then succeeds.

Example on the command line:


Code
perl -e '$c = "aaabba"; print "matched \"$1\"\n" if  $c =~ /^(a*).*\1$/;' 
matched "a"