应用许多 Perl 正则表达式测试的最佳方法是什么? [英] What's the best way to apply many Perl regular expression tests?

查看:47
本文介绍了应用许多 Perl 正则表达式测试的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Perl 模块,可以将文本与数百个正则表达式列表进行匹配;目前我只是或'ing他们:

I have a Perl module that matches text against a list of hundreds of regexes; currently I am just or'ing them:

if (
  /?:re1/ or
  ...
  /re200$/
) { return "blah"; }

是否有更好/更快/更少资源密集型的方法来做到这一点?也许存在有用的模块,或者我应该将它们存储在散列中,等等.

Is there a better/faster/less resource-intensive way to do this? Perhaps a useful module exists, or I should store them in a hash, etc.

推荐答案

看一看 Regexp::Assemble.

这是来自描述:

Regexp::Assemble 采用任意数量的正则表达式,并将它们组合成一个正则表达式(或 RE),与各个 RE 匹配的所有内容相匹配.

Regexp::Assemble takes an arbitrary number of regular expressions and assembles them into a single regular expression (or RE) that matches all that the individual REs match.

因此,不需要循环遍历大量表达式,目标字符串只需要针对一个表达式进行测试.当您要处理数千种模式时,这很有趣.尽最大努力制作出尽可能小的图案.

As a result, instead of having a large list of expressions to loop over, a target string only needs to be tested against one expression. This is interesting when you have several thousand patterns to deal with. Serious effort is made to produce the smallest pattern possible.

还可以跟踪原始模式,以便您可以确定在构成组合模式的源模式中哪个是导致匹配发生的模式.

It is also possible to track the original patterns, so that you can determine which, among the source patterns that form the assembled pattern, was the one that caused the match to occur.

我将它用于一些项目,效果非常好.

I used it for some projects and it was pretty amazing.

这篇关于应用许多 Perl 正则表达式测试的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆