Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to define a list of regular expressions?

by pat_mc (Pilgrim)
on Jul 01, 2009 at 09:48 UTC ( #776365=perlquestion: print w/ replies, xml ) Need Help??
pat_mc has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks!

I have the following question for ye today: How can I define a list of regular expressions over which I subsequently want to iterate?

So far, I have tried the following, alas to no avail:
my @files = ( "File1", "File2", ... ); my @regexes = ( 'verb [\w<>]+\s+/', 'noun \w+\s+/', ... ); for my $file ( @files ) { my $regex = qr/shift @regexes/; ... $sizes{ $version }{ $file } ++ if /$regex/; }

Syntax highlighting in my editor already informs me that this is syntactically unacceptable. I also tried various ways of quoting the entries in the list ... but none of them worked. The error message received is
Use of uninitialized value $regex in regexp compilation at ./compare.l +exicons.pl line ...
Presumably, it is just a small thing I am overlooking -- but I simply cannot get it. Ideally, what I want is a list of compiled regular expressions that I can simply fetch using  shift and interpolate where needed. Your help in this matter hence would be much appreciated.

Thanks a lot in advance and best regards -

Pat

Comment on How to define a list of regular expressions?
Select or Download Code
Re: How to define a list of regular expressions?
by Anonymous Monk on Jul 01, 2009 at 09:56 UTC
    qr is quoting operator
    my @regexes = ( qr'verb [\w<>]+\s+/', qr'noun \w+\s+/', ... ); for my $re (@regexes ){ $sizes{ $version }{ $file } ++ if /$re/; }
      Thanks, Anonymous Monk ...

      What, though, if I do not want to loop over the regexes but retrieve them from the list via shift when needed? Can you point out to me where or why my code is broken?

      Thanks - Pat
        Instead of
        my $regex = qr/shift @regexes/;
        do just
        my $regex = shift @regexes;
Re: How to define a list of regular expressions?
by JavaFan (Canon) on Jul 01, 2009 at 10:03 UTC
    Syntax highlighting in my editor already informs me that this is syntactically unacceptable.
    I'm curious. I don't spot a syntax error in your code fragment (except for the '...' parts).
      Very observant of you, JavaFan - I misinterpreted my editor's change in highlighting colour inside the slashes which was due to the fact that the quote contained a variable, not a syntax error. Please ignore my comment on the syntax error.

      I don't know a single editor that can properly syntax-highlight arbitary perl code. Some editors are rather clever, but some are already confused when the code contains POD. Quotes in regular expressions with slash-replacements confuse all editors I know.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Quotes in regular expressions with slash-replacements confuse all editors I know.

        You must not know Scintilla based editors (scite/notepad++...) or jEdit :)

Re: How to define a list of regular expressions?
by rovf (Priest) on Jul 01, 2009 at 11:39 UTC
    Use of uninitialized value $regex

    My guess: In your code you initially have @files > @regexes.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: How to define a list of regular expressions?
by AnomalousMonk (Abbot) on Jul 01, 2009 at 13:08 UTC
    And, to avoid looping through all the regexes, it's possible to combine the individual regexes into a single regex (probably a big performance win if there are many regexes):
    >perl -wMstrict -le "my @regexes = ( qr{ foo }xms, qr{ bar }xms, qr{ baz }xms, ); my $any_regex = qr{ @{[ join '|', @regexes ]} }xms; for my $file (@ARGV) { print $file, $file =~ $any_regex ? ' MATCHES' : ' no match'; } " xfooy snork bar xfooy MATCHES snork no match bar MATCHES
    Remember, however, that Perl's  | (vertical bar) regex alternation operator provides ordered alternation: the overall alternation will match with whatever sub-pattern first matches in the alternation sequence, regardless of length. See Matching this or that in perlretut.

      See Regexp::Assemble

      #!/usr/bin/perl use strict; use warnings; use Regexp::Assemble; my $ra = Regexp::Assemble->new->add(qw[foo bar baz]); print $ra->re, "\n"; for (qw[xfooy snork bar]) { print " $_ does " . ( /$ra/ ? '' : 'not ' ) . "match\n"; } __END__ Output: (?-xism:(?:ba[rz]|foo)) xfooy does match snork does not match bar does match


      hth,
      PooLpi

      'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://776365]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2014-10-01 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls