sas has asked for the wisdom of the Perl Monks concerning the following question:
for me (Perl v5.8.6 linux) it produces the following output;my $str=" some stuff then foo then bar then more stuff"; print "string=\"$str\"\n"; if ($str =~ /(?!.*foo)(^.*bar.*)/) {print "1 matched \"$1\"\n";} if ($str =~ /(?!^.*foo)(^.*bar.*)/) {print "2 matched \"$1\"\n";} if ($str =~ /(?!.*foo)(.*bar.*)/) {print "3 matched \"$1\"\n";} if ($str =~ /(?!^.*foo)(.*bar.*)/) {print "4 matched \"$1\"\n";} $str=" some stuff then bar then more stuff"; print "string=\"$str\"\n"; if ($str =~ /(?!.*foo)(^.*bar.*)/) {print "5 matched \"$1\"\n";} if ($str =~ /(?!^.*foo)(^.*bar.*)/) {print "6 matched \"$1\"\n";} if ($str =~ /(?!.*foo)(.*bar.*)/) {print "7 matched \"$1\"\n";} if ($str =~ /(?!^.*foo)(.*bar.*)/) {print "8 matched \"$1\"\n";}
I don't know a lot about Perl regex can someone explain why 3 and 4 above matched and why they matched was the did?string=" some stuff then foo then bar then more stuff" 3 matched "oo then bar then more stuff" 4 matched "some stuff then foo then bar then more stuff" string=" some stuff then bar then more stuff" 5 matched " some stuff then bar then more stuff" 6 matched " some stuff then bar then more stuff" 7 matched " some stuff then bar then more stuff" 8 matched " some stuff then bar then more stuff"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Negative Lookahead Assertion Strangness
by merlyn (Sage) on May 15, 2006 at 16:34 UTC | |
by Fletch (Bishop) on May 15, 2006 at 17:32 UTC | |
|
Re: Negative Lookahead Assertion Strangness
by kvale (Monsignor) on May 15, 2006 at 16:37 UTC | |
|
Re: Negative Lookahead Assertion Strangness
by ikegami (Patriarch) on May 15, 2006 at 17:04 UTC | |
|
Re: Negative Lookahead Assertion Strangness
by TedPride (Priest) on May 15, 2006 at 19:12 UTC | |
|
Re: Negative Lookahead Assertion Strangness
by MonkE (Hermit) on May 15, 2006 at 17:17 UTC |