Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: On zero-width negative lookahead assertions

by antirice (Priest)
on Sep 10, 2004 at 15:16 UTC ( [id://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*(?!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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-16 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found