Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Returning _which_ pattern matched...?

by lestrrat (Deacon)
on Aug 03, 2001 at 18:53 UTC ( [id://101996]=note: print w/replies, xml ) Need Help??


in reply to Returning _which_ pattern matched...?

If you happen to have a lot of patterns to match, I like the "build a sub on the fly" approach

my @patterns = qw/ foo bar baz /; ## if you need to return the pattern itself, return '$_' instead my $match_n_return = join( "\n", map{ "return \$1 if \$_[0] =~ /$_/o;" } @patterns ); my $sub = eval qq|sub { $match_n_return return undef }|; if( $@ ) { ## some syntax error in the eval string die $@ } my $matched = $sub->( "string you want to match" );

The only problem here is, you don't get a fine granularity of control for each match. But I like the fact that this can be used over and over, and that since all the regexes are pre-compiled, you save some time for repeated use

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://101996]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-18 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found