Update:
As
Hofmator was kind enough to point out, I was confusing
qr with
quotemeta, which are two entirely different
beasties, though for some reason I had the two confused.
Using \Q and \E is still one way to avoid having to
go backslash crazy on your regex, though, and this was
the original inspiration for this follow-up.
The original post is preserved below.
Using qr() on \b is going to look for "backslash,b" instead
of a word break. Try this instead:
$ip = "192.168.3.5";
# ...
if ($line =~ /\b\Q$ip\E\b/)
{
# ...
}
Using \Q and \E ensure that no matter what $ip has in it,
it will be matched very literally. You could also take this
approach:
$ip = qr("192.168.3.5");
# ...
if ($line =~ /\b$ip\b/)
{
# ...
}
This makes sure that $ip is "regex-ready" before being
used, by virtue of the qr() function.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.