Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Non-greedy regex behaves greedily

by kovacsbv (Novice)
on Jul 27, 2008 at 23:36 UTC ( [id://700434]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://700434]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found