in reply to On zero-width negative lookahead assertions
A few things:
- Don't forget to escape your @ and ..
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.
- \s* can also match the empty string as the following code shows:
> perl -l $_='root: email@example.com'; print '(',join(")(",/^(root:)(\s*)(?!admin\@somewhere\.here)/),')'; print qq[Postmatch contained "$'"]; __END__ (root:)() Postmatch contained " firstname.lastname@example.org"
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 = 'email@example.com'; /^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
The ith rule of Perl club is - follow rule i - 1 for i > 1