Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: On zero-width negative lookahead assertions

by antirice (Priest)
on Sep 10, 2004 at 15:16 UTC ( #390109=note: print w/replies, xml ) Need Help??

in reply to On zero-width negative lookahead assertions

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*(?!; 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:'; print '(',join(")(",/^(root:)(\s*)(?!admin\@somewhere\.here)/),')'; print qq[Postmatch contained "$'"]; __END__ (root:)() Postmatch contained ""
  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 = ''; /^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 =/

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://390109]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2022-01-21 21:16 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (59 votes). Check out past polls.