in reply to Re: Saving Results of a while loop
in thread Saving Results of a while loop

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.

Replies are listed 'Best First'.
Re3: Saving Results of a while loop
by Hofmator (Curate) on Aug 01, 2001 at 20:02 UTC

    Well, I have to say no and no

    1. qr// works fine with \b, e.g.
      my $string = q/This is a test./; my $regex1 = qr(\bis); my $regex2 = qr(is); print $string =~ /$regex1/g, "\n"; print $string =~ /$regex2/g, "\n"; # prints is isis
    2. Your second part
      $ip = qr("192.168.3.5");
      matches e.g. "192 16833!5" but not what was being looked for. Did you maybe want to use the quotemeta function?? Then
      my $ip = "192.168.3.5"; $ip = quotemeta $ip;
      would be the way to go.

    -- Hofmator

      quotemeta would be the one. Looks like I got a little carried away there. In some of my programs I've aliased "qm" to be "quotemeta", which would account for some of the confusion on my part. The rest I can just chalk up to plain old fashioned ignorance.

      Thanks for pointing this out.