pl@nereida:~/Lperltesting$ cat comparequotedstrings.pl #!/usr/local/lib/perl/5.10.1/bin//perl5.10.1 use v5.10; use Benchmark qw{cmpthese}; # See http://www.regex-engineer.org/slides/img10.html my $regexp = qr/ " # double quote (?: # no memory [^"\\]++ # no " or escape: Don't backtrack | (?: \\.)++ # escaped character )*+ # Don't backtrack " # end double quote /x; my $bregexp = qr/ " # double quote (?: # no memory [^"\\]+ # no " or escape: backtrack | (?: \\.)+ # escaped character )* # backtrack " # end double quote /x; # input matches many times. Using "g" option my $input = (q{"abc\"defg"hijk}x10000); cmpthese( 0, { gbacktrack => q{ $input =~ /$bregexp/g }, gpossessive => q{ $input =~ /$regexp/g } } ); #input matches a long string, no g option $input = '"'.(q{abc\"defg}x10000).'"'; cmpthese( 0, { backtrack => q{ $input =~ /$bregexp/ }, possessive => q{ $input =~ /$regexp/ } } ); # input does not match. Using "g" option $input = '"'.("abcdefghijk"x10000); cmpthese( 0, { failgbacktrack => q{ $input =~ /$bregexp/g }, failgpossessive => q{ $input =~ /$regexp/g } } );