The accepted answer seems a bit complicated if you are just trying to match the closest โbโ to the โdogโ, including the dog, you just need to make your matches before you look for greedy ones. For instance:
# First example my $string1 = 'abcbdog'; if ( $string1 =~ /.+(b.*dog)/ ) { print $1;
Or am I missing something? If you want to change the captured line according to what you want, just shift the brackets.
Rohaq source share