如何在Perl中与多个正则表达式匹配? [英] 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?
推荐答案
我一次测试多个正则表达式的值的方法是track开关将提供该信息.它的性能非常好-在一个应用程序中,我正在使用它同时针对1700个模式进行测试-而且我还不需要它不需要做的任何事情.
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.
这篇关于如何在Perl中与多个正则表达式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!