in reply to Regex latest match?
For simple cases, you could use this regexp:
while( $string =~ m/\bstart:(.+?)\bstop:/g ) { print "$1\n"; }
...and to get the last match using that regexp, apply it like this:
my $lastmatch = ( $string =~ m/\bstart:(.+?)\bstop:/g )[ -1 ];
<update>
...or for a pure regexp approach:
if( $string =~ m/\bstart:(?!.+?(?:\bstart:))(.+?)\bstop:/ ) { print "$1\n"; }
...which only further punctuates the point I make in the next paragraph.
</update>
But that's not going to be very robust. What if your needs mature to the point that you can no longer guarantee that 'stop:' doesn't occur embedded within the portion of the string you're capturing? For example, if 'stop:' is wrapped in quotes, should it be treated as a delimiter, or as text? For a solution that will stand up to these sorts of complex strings, forget about hand crafting a masterful regular expression. The hard work has already been done, refined, debugged, tested, and proven. To take advantage of the work that's already been done, have a look at Text::Balanced.
Dave
|
|---|