合并多个正则表达式到一个单一 [英] Merge several regexes to a single one

查看:1652
本文介绍了合并多个正则表达式到一个单一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个正则表达式(实际上是几千),我必须检查,如果一个字符串匹配任何上述正则表达式中。这是不是很有效,所以我想合并所有这些正则表达式作为一个单一的正则表达式。

I have several regexes (actually several thousands), and I must check if one string matches any of these regexes. It is not very efficient, so I would like to merge all these regexes as a single regex.

例如,如果有这些正则表达式:

For example, if a have these regexes:

  • 在'富*酒吧
  • 在'富*拉链
  • 在ZAP *酒吧

我想获得像'富*(栏| ZIP)| ZAP *吧。

I would like to obtain something like 'foo *(bar|zip)|zap *bar'.

有一些算法,库或工具来做到这一点?

Is there some algorithm, library or tool to do this?

推荐答案

您可以只使用串联的正则表达式或( | )(和锚的开始/结束字符串)。

You can just concatenate the regexes using or (|) (and anchors for the beginning/end of string).

大多数优秀的正则表达式库优化其有限状态自动机后,他们从你的正则表达式生成它。 PCRE这样做,例如。

Most good regex libraries optimize their finite state automata after they build it from your regex. PCRE does that, for instance.

这一步通常需要你的优化问题,即照顾。它们适用大多数的变换,你必须做的手。

This step usually takes care of your optimization problem, ie. they apply most of the transformations you would have to do "by hand".

这篇关于合并多个正则表达式到一个单一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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