A few things:

  1. Don't forget to escape your @ and ..
  2. I also tried escaping the @ sign with a backslash, \Q...\E or useing strict: no way.

    You must escape @ in a regex no matter what. However, be careful with your escaping as it exhibits different behavior depending upon what's around it.

    > perl -l $,=$/; print 'right:', qr(a\@b), qr(a\Q@\Eb), qr(\Qa@\Eb), qr(\Qa\E\@\Qb\E); print 'wrong:', qr(\Qa@b\E), qr(\Qa\@b\E); __END__ right: (?-xism:a\@b) (?-xism:a\@b) (?-xism:a\@b) (?-xism:a\@b) wrong: (?-xism:a) (?-xism:a\\\@b)

    Your regex without an escaped @ is equivalent to /^root:\s*(?!admin.here)/; that is unless @somewhere is defined within your program, of course.

  3. \s* can also match the empty string as the following code shows:
    > perl -l $_='root: admin@somewhere.here'; print '(',join(")(",/^(root:)(\s*)(?!admin\@somewhere\.here)/),')'; print qq[Postmatch contained "$'"]; __END__ (root:)() Postmatch contained " admin@somewhere.here"
  4. More oddly (to me), if I add a \s*$ at the end of the regex to match any whitespace between the address and the end of line, then no line matches!!!

    The reason for that is because you basically turned your regex into /^root:\s*$/.

How should you do it? There are a couple of ways:

Hardcoded: /^root:(?!\s*admin\@somewhere\.here)/ Variable: my $admin_email = 'admin@somewhere.here'; /^root:(?!\s*\Q$admin_email\E)/

Note that if you want more constraints on your regex, you need to add them at the end of the zero-width negative lookahead assertion. Hope this helps.

Update: Wow, guess that took me a lot longer than I thought it would. Everyone else already said what I did =/

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1


In reply to Re: On zero-width negative lookahead assertions by antirice
in thread On zero-width negative lookahead assertions by bronto

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.