检查两个 Python 正则表达式模式是否等效 [英] Checking if two Python regex patterns are equivalent

查看:53
本文介绍了检查两个 Python 正则表达式模式是否等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 re.VERBOSE 模式,但我不确定我会添加详细部分而不会出错.

我记得,理论上,两个正则表达式的等价性(至少没有反向引用)可以通过生成它们的自动机并尝试找到图双射来找到.但是我看不到用于比较正则表达式的实例方法.

有没有办法生成正则表达式的自动机或直接比较它们,最好与标准库进行比较?

(我已经决定采用不同的解决方案来解决我的问题,但这仍然是我感兴趣的.)

解决方案

您可以使用未公开的 re.DEBUG 功能:

<预><代码>>>>r1 = re.compile("foo[bar]baz", re.DEBUG)文字 102文字 111文字 111在字面量 98字面量 97文字 114字面量 98字面量 97文字 122>>>r2 = re.compile("""foo # foo!... [bar] # b 或 a 或 r!... baz # baz!""", re.VERBOSE|re.DEBUG)文字 102文字 111文字 111在字面量 98字面量 97文字 114字面量 98字面量 97文字 122

如果输出相同,r1r2 也相同.

I want to write a regex in re.VERBOSE mode, but I'm not confident that I'll add the verbose part without error.

I remember that, theoretically, the equivalence of two regexes (without backreferences, at least) can be found by generating their automata and trying to find a graph bijection. But there's no instance method I can see for comparing regexes.

Is there a way to either generate the automaton of a regex or directly compare them, preferably with the standard library?

(I've already decided on a different solution to my problem, but this is still of interest to me.)

解决方案

You can use the undocumented re.DEBUG feature:

>>> r1 = re.compile("foo[bar]baz", re.DEBUG)
literal 102
literal 111
literal 111
in
  literal 98
  literal 97
  literal 114
literal 98
literal 97
literal 122
>>> r2 = re.compile("""foo   # foo!
...                    [bar] # b or a or r!
...                    baz   # baz!""", re.VERBOSE|re.DEBUG)
literal 102
literal 111
literal 111
in
  literal 98
  literal 97
  literal 114
literal 98
literal 97
literal 122

If the output is identical, r1 and r2 are identical as well.

这篇关于检查两个 Python 正则表达式模式是否等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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