Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Multiple matching using m///

by wog (Curate)
on Sep 17, 2001 at 04:24 UTC ( [id://112784]=note: print w/replies, xml ) Need Help??


in reply to Multiple matching using m///

The easiest way to detect if something matches two patterns is to just try both pattern matches, for example:

if ($line =~ m/balls/ and $line =~ m/rings/) { # ... }

This can be done with one regex, too, but it's much more complicated (and probably slower) and more difficult to maintain, for example:

m/balls.*?rings|rings.*?balls/s

update: A better technique to combine the two regexes into one is presented in chipmunk's reply to this node. It's not only more maintainable, but it's probably faster then my regex here, at least.

update 2: I have benchmarked my combined regex, chipmunk's regex, and just using two regexes. (All tuned for matching "rings" and "balls" of course.) Using two regexes is signigantly faster (by 30% for the string "ringsballs" with that factor getting much larger the longer the searched string is.) My combined regex tends to be the slowest of them all, and chipmunk's comes in second. (though performance was close to my combined regex when the string being searched started with one of the strings "rings" or "balls" and there was a lot of junk text in the string being matched.)

(The benchmark was performed on strings composed of a number of random characters, followed by "rings" followed by a number of random characters followed by "balls" followed by a number of random characters, and the same without the first set of random characters. Random characters were all selected from lowercase letters a through z.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found