David B David B - 5 months ago 6x
Perl Question

How can I match against multiple regexes in Perl?

I would like to check whether some string match any of a given set of regexes. How can I do that?


My go-to for testing a value against multiple regexes at once is Regexp::Assemble, which will "Assemble multiple Regular Expressions into a single RE" in a manner somewhat more intelligent and optimized than simply doing a join '|', @regexps. You are also able, by default, to retrieve the portion of the text which matched and, if you need to know which pattern matched, the track switch will provide that information. Its performance is quite good - in one application, I'm using it to test against 1700 patterns at once - and I have yet to need anything that it doesn't do.