I would like to check whether some string match any of a given set of regexes. How can I do that?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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, thetrackswitch 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.