Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Configurable Matches

by Stevie-O (Friar)
on Jun 03, 2004 at 04:17 UTC ( #359916=note: print w/ replies, xml ) Need Help??


in reply to Configurable Matches

It's a lot easier than you might think... m// interpolates.

#!/usr/bin/perl -l chomp($tmp = <DATA>); $foo = 'the rain in spain'; print "match" if $foo =~ /$tmp/; __END__ __DATA__ ain$
Change the line after __DATA__ to change the regex matched against.
--Stevie-O
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc


Comment on Re: Configurable Matches
Select or Download Code
Re^2: Configurable Matches
by jdhawke (Acolyte) on Jun 03, 2004 at 04:41 UTC
    Hmmm, this makes it even easier, any pros/cons of doing it this way vs the qr// method that Mr.Muskrat demonstrated above?
      Well, you hit the pro on the head... it's much easier to interpolate variables into m//.

      There is a major con, though. If you plan on using this regular expression in a loop, it will need to be recompiled every time it is used. With qr//, you will only compile once... You may want to benchmark them to find out the difference:

      #!/usr/bin/perl -w use Benchmark; my $txt = 'find this regex'; my $str = 'regex?'; my $rgx = qr/$str/i; timethese ( 1000000, { match => sub{ $txt =~ m/$str/i }, qreg => sub{ $txt =~ $rgx }, }, );

      Gives me the following:

      % test.pl Benchmark: timing 1000000 iterations of match, qreg... match: 1 wallclock secs ( 0.82 usr + 0.00 sys = 0.82 CPU) @ 12 +19512.20/s (n=1000000) qreg: 0 wallclock secs ( 0.73 usr + 0.00 sys = 0.73 CPU) @ 13 +69863.01/s (n=1000000)

      As you can see, for a regex this simple, it doesn't matter too much... but more complicated regexs wll show a bigger difference.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2015-07-06 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls