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

I am trying (big emphasis on trying ) to write a script to parse log files, I thought it would be a good idea to store the regex in a hash table and use the event ID as the key. So that I can add to the hash table as new log messages types are added. My problem is that I get the regex string working properly but when I put it in a variable it stops working, a quick example is the regex to extract the event ID this works: $current_line =~ q{\A.*? %(.*?): }xms; $event_id = $1; this fails: $RGX_EVENTID = 'q{\A.*? %(.*?): }xms'; $current_line =~ $RGX_EVENTID; $event_id = $1; I'm not understanding what I'm doing wrong. Sal

Replies are listed 'Best First'.
Re: store regex in hash
by AnomalousMonk (Archbishop) on Jul 03, 2011 at 20:16 UTC

    The lack of follow-up questions from salp suggests he or she has the "store the regex in a hash" part under control, but just in case:

    >perl -wMstrict -le "my %rx = ( FOO => qr{ (?i) foo (?! bar) }xms, BAR => qr{ (?i) bar (?= (?-i) foo) }xms, ); ;; my $s = 'xxx FooBar fOO BaRFoO bArfoo bar yyy'; for my $type (keys %rx) { print qq{parsing $type-ish stuff with $rx{$type}}; while ($s =~ m{ ($rx{$type}) }xmsg) { print qq{ found a '$1'}; } } " parsing BAR-ish stuff with (?msx-i: (?i) bar (?= (?-i) foo) ) found a 'bAr' parsing FOO-ish stuff with (?msx-i: (?i) foo (?! bar) ) found a 'fOO' found a 'FoO' found a 'foo'

      That works, thanks to everyone.

      PS: JavaFan is correct the code I posted would not compile I mistyped it in the post

Re: store regex in hash
by salp (Novice) on Jul 03, 2011 at 17:37 UTC
    Sorry didnt read how to format post

    I am trying (big emphasis on trying ) to write a script to parse log files, I thought it would be a good idea to store the regex in a hash table and use the event ID as the key. So that I can add to the hash table as new log messages types are added.

    My problem is that I get the regex string working properly but when I put it in a variable it stops working, a quick example is the regex to extract the event ID

    #this works: $current_line =~ q{\A.*? %(.*?): }xms; $event_id = $1; #this fails: $RGX_EVENTID = 'q{\A.*? %(.*?): }xms'; $current_line =~ $RGX_EVENTID; $event_id = $1;
      Try it like this:
      $RGX_EVENTID = qr{\A.*? %(.*?): }xms; if ( $current_line =~ /$RGX_EVENTID/ ) { print "got event_id //$1// from input //$_//\n"; }
      Note the use of the qr operator to assign a regex-like string to $RGX_EVENTID, and the use of normal regex match delimiters around that variable when applying the match to another string. (You need to be careful about using regex delimiters that either don't occur within the regex itself, or are properly escaped when they do occur.)

      Also, I think it's better to update your OP to fix the formatting there, rather than creating a reply that fixes the formatting.

      Update: forgot to mention... Welcome to the Monastery! "OP" refers to "original post" (top of the thread).

        and the use of normal regex match delimiters around that variable when applying the match to another string.

        The use of regex match delimiters is not actually necessary when matching against a string as the binding operator provides the context to use the compiled regex. They are needed if doing a default match against $_ without a binding operator.

        knoppix@Microknoppix:~$ perl -E ' > $rx = qr{(\d+)}; > > $str = q{ab12ef}; > $_ = q{gh34kl}; > > say $1 if $str =~ $rx; > say $1 if $rx; > say $1 if m{$rx}; > > $_ = q{mn56qr}; > > say $1 if $_ =~ $rx;' 12 12 34 56 knoppix@Microknoppix:~$

        I hope this is of interest.

        Cheers,

        JohnGG

      #this works: $current_line =~ q{\A.*? %(.*?): }xms; $event_id = $1;
      Really? I don't think it even compiles.