http://www.perlmonks.org?node_id=1027893


in reply to Puzzled by regex

There is a difference, but it's not necessarily in what is getting matched, but rather, in how it's matching. Example:

use strict; use warnings; my @strings = ( "____\n", "__ __\n", "__X __\n", "__Z__\n", "__\n__\n", "__^__\n", "_____\n", "________\n", ); foreach my $string ( @strings ) { print "<<$string>>\n"; if( $string =~ m/__(\S+?)__/ ) { print "\tNon-Greedy -- Match: (($1)).\n"; } else { print "\tNon-Greedy -- No Match.\n"; } if( $string =~ m/__(\S+)__/ ) { print "\tGreedy -- Match: [[$1]].\n"; } else { print "\tGreedy -- No Match.\n"; } }

Most of that is going to be pretty boring, until you get to the last item in the list, where you'll get the following output:

<<________ >> Non-Greedy -- Match: ((_)). Greedy -- Match: [[____]].

I have no idea whether non-greedy matching is going to have any practical effect in the type of strings you're matching with the regex though.


Dave