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: admin@somewhere.here';
print '(',join(")(",/^(root:)(\s*)(?!admin\@somewhere\.here)/),')';
print qq[Postmatch contained "$'"];
__END__
(root:)()
Postmatch contained " admin@somewhere.here"
-
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|