如何在 Python 中调试正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/606350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I debug a regular expression in Python?
提问by Geo
Is there a way to debug a regular expression in Python? And I'm not referring to the process of trying and trying till they work :)
有没有办法在 Python 中调试正则表达式?而且我不是指尝试和尝试直到它们起作用的过程:)
Here is how regexes can be debugged in Perl:
下面是在 Perl 中调试正则表达式的方法:
use re 'debug';
my $str = "GET http://some-site.com HTTP/1.1";
if($str =~/get\s+(\S+)/i) {
print "MATCH:\n";
}
The code above produces the following output on my computer when ran:
运行时,上面的代码在我的计算机上产生以下输出:
Compiling REx "get\s+(\S+)"
Final program:
1: EXACTF (3)
3: PLUS (5)
4: SPACE (0)
5: OPEN1 (7)
7: PLUS (9)
8: NSPACE (0)
9: CLOSE1 (11)
11: END (0)
stclass EXACTF minlen 5
Matching REx "get\s+(\S+)" against "GET http://some-site.com HTTP/1.1"
Matching stclass EXACTF against "GET http://some-site.com HTTP/1.1" (33 chars)
0 | 1:EXACTF (3)
3 | 3:PLUS(5)
SPACE can match 1 times out of 2147483647...
4 | 5: OPEN1(7)
4 | 7: PLUS(9)
NSPACE can match 20 times out of 2147483647...
24 | 9: CLOSE1(11)
24 | 11: END(0)
Match successful!
MATCH:http://some-site.com
Freeing REx: "get\s+(\S+)"
采纳答案by Mykola Kharechko
>>> p = re.compile('.*', re.DEBUG)
max_repeat 0 65535
any None
>>>
回答by Nikita G.
https://www.debuggex.comis also pretty good. It's an online Python (and a couple more languages) debugger, which has a pretty neat visualization of what does and what doesn't match. A pretty good resource if you need to draft a regexp quickly.
https://www.debuggex.com也不错。这是一个在线 Python(和更多语言)调试器,它对匹配和不匹配的内容进行了非常简洁的可视化。如果您需要快速起草正则表达式,这是一个非常好的资源。
回答by sabiland
回答by Noldorin
Not sure about doing such a thing directly in Python, but I could definitely suggest using a RegEx editor tool. That's likely to be your best bet anyway. Personally, I've used The Regulatorand found it to very helpful. Some others are listed in this SO thread.
不确定直接在 Python 中做这样的事情,但我绝对可以建议使用 RegEx 编辑器工具。无论如何,这可能是您最好的选择。就个人而言,我使用过The Regulator并发现它非常有帮助。其他一些列在此 SO 线程中。
回答by Rook
Similar to the already mentioned, there is also Regexbuddy
类似于已经提到的,还有Regexbuddy
回答by Jon Cage
I quite often use RegexPalfor quick checks (an online regular expression prototyper). It has a lot of the common expressions listed along with a simple expression. Very handy when you don't have a dedicated tool and just need a quick way to work out a somple regex.
我经常使用RegexPal进行快速检查(一个在线正则表达式原型)。它列出了许多常用表达式以及一个简单的表达式。当您没有专用工具并且只需要一种快速的方法来计算简单的正则表达式时非常方便。
回答by Jan Goyvaerts
What RegexBuddy has that the other tools don't have is a built-in debuggerthat shows you the entire matching process of both successful and failed match attempts. The other tools only show the final result (which RegexBuddy can show too).
RegexBuddy 拥有而其他工具没有的是一个内置调试器,它向您显示成功和失败的匹配尝试的整个匹配过程。其他工具只显示最终结果(RegexBuddy 也可以显示)。