in reply to Regular Expression Matching
Well, you seem to have a mistaken notion of how non-greedy matching operates (but that's not a rare problem). Consider using a negative character class like: /HREF="([^"]+)"/ instead (assuming you won't find an escaped double-quote in your href).
Then, one way is to precede your pattern with .* and let greediness and backtracking take care of ensuring your match is the last one on the line:
my $line = q|a HREF="../../main.shtml" a HREF="index0002.shtml" Next P +age|; if($line =~ /.*HREF="([^"]+)"/){ print "$1\n"; }
Alternatively, you can wrap the match operator in parens (to put it into list context) and use the /g modifier to find all the matches, and then index just the final element in the return list:
my $line = q|a HREF="../../main.shtml" a HREF="index0002.shtml" Next P +age|; if(my $link = ($line =~ /HREF="([^"]+)"/g)[-1]){ print "$link\n"; }
|
|---|