in reply to Re^2: Non-greedy regex behaves greedily
in thread Non-greedy regex behaves greedily

Ok, is there a nice detailed description of the engine that would fill in what causes a second run and what the "?" does exactly?

This behavior isn't very intuitive.

Also, is there another way to get the desired result other than the ugly hack I posted below?
  • Comment on Re^3: Non-greedy regex behaves greedily

Replies are listed 'Best First'.
Re^4: Non-greedy regex behaves greedily
by ysth (Canon) on Jul 28, 2008 at 10:03 UTC
    You can force the regex engine to start looking for </a> at the end of the string and work forwards by consuming all the string to start with and backtracking character by character:
    my $string = q!Back to STATES Menu</font></a></h3> <p align="center">< +a href="index.htm"><img src="home2.gif" alt="Home" border="0" width=" +106" height="30"></a></p> </body> </html>!; if ( $string =~ m!^.*(</a>.*?)$! ) { print "got $1\n"; }
    but there's usually a better way to get what you want done.