in reply to RegEx Confusion

It prints 1 for me, as I expected. If you run the program below and it prints 1 for you as well, something you said is not true. Find out what, and you'll find your problem.

$dev_ref->{hostname} = '2383-RWAN-1'; $dev_ref->{grbr } = '2383'; my $pattern = qr{HOSTNAME:\s*(?:\d{4}-)?$dev_ref->{hostname}\s+GRBR:\s +*$dev_ref->{grbr}}i; my $buffer = <<'__EOI__'; 9600/ARQ Some Company ---> Unauthorized use of this router is prohibited <--- ******************************************************* * Hostname: 2383-rwan-1 GRBR: 2383 * * Model: Cisco 1234 * * Location: Ansalon , DL * ******************************************************* ---> Unauthorized Access is strictly prohibited <--- User Access Verification Username: __EOI__ my $hostname = $buffer =~ m/$pattern/i ? 1 : 0 ; print($hostname, "\n");

By the way, the i option on m/$pattern/i is useless, since it's already on for $pattern. Also, you should probably escape what you putt in from $dev_ref:
qr{HOSTNAME:\s*(?:\d{4}-)?\Q$dev_ref->{hostname}\E\s+GRBR:\s*\Q$dev_ref->{grbr}\E}i
Neither change would affect the result here, though.

Replies are listed 'Best First'.
Re^2: RegEx Confusion
by Argel (Prior) on Sep 21, 2005 at 20:05 UTC
    Yep, same result here. Hmm, I must be getting some special characters back from the routers. Thanks for the tip!!! Time to dig out tohex (after I get done beating my head against a wall of course! :-)

    P.S. I left the 'i' on as a reminder when reading through the code. I wonder what TheDamian would recommend?!

    -- Argel

      Good luck. About the "i", keep in mind that the following won't match:

      $pattern = qr/A/; if ('a' =~ m/$pattern/i) { print("Match (case-insensitive)\n"); } else { print("No match (case-sensitive)\n"); }

      Whatever is or isn't on the qr// overrides what is or isn't on the m//. The "i" is misleading. If you want, you could drop the m// altogether:

      $pattern = qr/A/; if ('a' =~ $pattern) { print("Match (case-insensitive)\n"); } else { print("No match (case-sensitive)\n"); }