negativ lookahead did not work as expected. let's focus on:
("gugus" =~ /gu(?!gu)/) ==> <gugus> found ("gu", "gu", "s")
and
("gugis" =~ /gu(?!gi)/) ==> <gugis> nomatch (undef, undef, undef)
do not solve the same.
Look at it this way:
"gugus" =~ /gu(?!gu)/
"gugus" "gugus" ^^ here and ^^ here
"gugus" "gugus" ^^ "gus" and ^^ "s"
"gugus" "gugus" ^^ "gus" yes! ^^ "s" no! => match fails, keep looking => match succeeds
Overall, the match succeeds at "gugus"
<update2> I realized the above might be a little misleading: The regex engine always works from left to right, so it does not execute the steps in the order I've shown here. It first fully inspects the leftmost match before continuing on to find the second "gu" in the string. </update2>
"gugis" =~ /gu(?!gi)/
"gugis" ^^ here
"gugis" ^^ "gis"
"gugis" ^^ "gis" yes! => match fails
"start" =~ /(?!start)/
v v v v v v (everywhere) " s t a r t "
"start" | "tart" | | "art" | | | "rt" | | | | "t" | | | | | "" v v v v v v " s t a r t "
"start" yes => fails | "tart" no => succeeds | | "art" no => succeeds | | | "rt" no => succeeds | | | | "t" no => succeeds | | | | | "" no => succeeds v v v v v v " s t a r t "
Side note: If you make the regex engine continue matching where it last left off with /g, you can see the whole thing in action:
$ perl -wMstrict -MData::Dump while ( "start" =~ /(?!start)/pg ) { dd ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH}; } __END__ ("s", "", "tart") ("st", "", "art") ("sta", "", "rt") ("star", "", "t") ("start", "", "")
If you could give some more complete examples of actual things you're trying to match and the actual regexes, that would probably help.
Minor edits for clarification.
In reply to Re^3: regex negativ lookahead
by haukex
in thread regex negativ lookahead
by SwissGeorge
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |