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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.