perl backreferences

They have: 36 posts

Joined: Oct 2004

This one should be simple (I hope).

$letters = "aaaabbbb";

$letters =~m/\w*(.)\1\w*/;
 
print "the letter $1 was found consecutively";
'

In the above, why isnt $1 set to "a" instead of "b".

The way i see it, the first match (\w*) would match the first "a".
The second match ((.)) would match the second "a".
The third match (\1) will only now match if it is the same as (.), which it is, i.e "a".
And the last match (\w*) would be taken care of by the remainder.

So why does perl skip the "a"'s and instead match on the "b"',.

TIA

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Remember, Perl is greedy when it looks for regular expressions. So it finds the longest (\w*) it can before it finds two letters in a row.

For example, change the string to "aabbccddee". You'll see that is finds the e's. That's because the match it can find where (\w*) has the longest value puts the ee's as the two in a row.

They have: 36 posts

Joined: Oct 2004

Thanks alot Tim. I get it now

Stu

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.