Argel has asked for the wisdom of the Perl Monks concerning the following question:

I'm in the process of doing a major overhaul (new features, etc.) on a perl app here and am running into a confusing RegEx issue. In the old, working code I have this RegEx:

m/HOSTNAME:\s*(\d{4}-)?$dev_ref->{hostname}\s+GRBR:\s*$dev_ref->{grbr}/i

However, in the new code this fails.

For debugging purposes I tried the following:

my $pattern = qr{HOSTNAME:\s*(?:\d{4}-)?$dev_ref->{hostname}\s+GRB +R:\s*$dev_ref->{grbr}}i; print "DEBUG: \$pattern='$pattern'\n" if $DEBUG; my $hostname = $buffer =~ m/$pattern/i ? 1 : 0 ;

And here is debug output from the new script (where $buffer is what is in between the ////// dividers):
B  U  F  F  E  R:
///////////////////////////////////////////////////////////////////////////////
 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:
///////////////////////////////////////////////////////////////////////////////
DEBUG: $pattern='(?i-xsm:HOSTNAME:\s*(?:\d{4}-)?2383-RWAN-1\s+GRBR:\s*2383)'
From tests in the code I know that $hostname is being set to 0. Any ideas why the match always fails in the new code? Note that I tried the exact same RegEx and test from the old script and it still fails. I also tried e.g. matching against '9600' and 'Username' and those also failed. Help!! :-/

UPDATED: Argh!!! I copied old code over to the new app but missed renaming one of the variables. SIGH.... Thanks for the comments and sorry I wasted your time.

-- Argel

Replies are listed 'Best First'.
Re: RegEx Confusion
by ikegami (Patriarch) on Sep 21, 2005 at 19:42 UTC

    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.

    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.

      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"); }
Re: RegEx Confusion
by Adrade (Pilgrim) on Sep 22, 2005 at 00:34 UTC
    Try adding an s modifier to the end of your precompiled regex, and try again., ie.,

    qr{HOSTNAME:\s*(?:\d{4}-)?$dev_ref->{hostname}\s+GRBR:\s*$dev_ref->{grbr}}is;

    -- Adam

    Update: Ya know - cancel that, I was having some regex confusion of my own :-)... your code works for me - I'd go with the suggestion to look for those special chars... If this might be the case, I'd instinctively go with a slightly cleaner something like:

    $pattern = qr|hostname: $dev_ref->{hostname}\s+GRB+R: $dev_ref->{grbr}|is;

    --
    By a scallop's forelocks!