CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



rajkumar
New User

Oct 9, 2012, 11:05 PM

Post #1 of 2 (6612 views)
Perl Regex: not processing back references correctly Can't Post

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

Post #2 of 2 (6607 views)
Re: [rajkumar] Perl Regex: not processing back references correctly [In reply to] Can't Post

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"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives