检查两个 Python 正则表达式模式是否等效 [英] Checking if two Python regex patterns are equivalent
问题描述
我想在 re.VERBOSE
模式,但我不确定我会添加详细部分而不会出错.
我记得,理论上,两个正则表达式的等价性(至少没有反向引用)可以通过生成它们的自动机并尝试找到图双射来找到.但是我看不到用于比较正则表达式的实例方法.
有没有办法生成正则表达式的自动机或直接比较它们,最好与标准库进行比较?
(我已经决定采用不同的解决方案来解决我的问题,但这仍然是我感兴趣的.)
您可以使用未公开的 re.DEBUG
功能:
如果输出相同,r1
和 r2
也相同.
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屋!