in reply to Multiple matching using m///
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/supdate: 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.)
|
|---|