http://qs1969.pair.com?node_id=1181557


in reply to Basic Regular expression

You've been given good answers already, but let me just add some details on the way the regex engine processes this string.
According to me, it should be "$1= This is Perl".
In fact, that's what is initially happening: the regex engine sees (.+) and grabs the whole string, i.e. "this is Perl". But then, it sees (e|r), so, in order for the whole regex to be successful, it has to backtrack and give back "l" and then "r", so that (e|r) can be successful. Note that this would happen even if you did not have capturing parentheses, so that the point is not so much that it is trying to populate $2, but that (e|r) has to match something for the whole regex to be successful.

Once it has matched the "r" with the second capture, the last part of the regex, (.*)$ can match the "l".