what did i not understand correctly?
You've already gotten some good answers, let me address this part: Lookaround Assertions are zero-width. So for example, /foo(?!bar)/ matches the same thing as /foo/, except that the next thing after the foo may not be bar, but it can be anything else, including the end of the string. So /(?!start|sidebar)/, because it matches zero characters, will match anywhere in the string where the next thing is not (start|sidebar), which is always true at the end of the string!*
tybalt89's solution addresses this by anchoring the match at the beginning of the string. You haven't explained the bigger picture of what you're trying to do, so currently I don't understand why you want to use negative lookahead instead of !~ /(start|sidebar)/ - it sounds to me like you want to use this regex as part of a bigger regex, in which case additional considerations might need to be taken if you want to use lookaround assertions. If you could explain the context, perhaps we could suggest additional solutions.
BTW, I like to test my regexes the way I showed in Re: How to ask better questions using Test::More and sample data.
* Update: And in fact it's true in most places in the string.
$ perl -wMstrict -MData::Dump "a"=~/(?!a)/p; dd ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH}; "start"=~/(?!start)/p; dd ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH}; __END__ ("a", "", "") ("s", "", "tart")
In reply to Re: regex negativ lookahead
by haukex
in thread regex negativ lookahead
by SwissGeorge
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |