【发布时间】:2009-03-03 13:13:45
【问题描述】:
有没有办法在 Python 中调试正则表达式?而且我指的不是尝试和尝试直到他们工作的过程:)
下面是如何在 Perl 中调试正则表达式:
use re 'debug';
my $str = "GET http://some-site.com HTTP/1.1";
if($str =~/get\s+(\S+)/i) {
print "MATCH:$1\n";
}
上面的代码在运行时会在我的计算机上产生以下输出:
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+)"
【问题讨论】:
-
说真的:给 SO 你的正则表达式 + 样本数据?
-
没有示例代码,我们只能猜测您的问题是什么。
-
我现在没有正则表达式问题。我只想知道如何调试。
-
也许上面的 cmets 暗示的是“要调试 python 正则表达式,请将您的 SO 发布到 stackoverflow.com,这样可怜的灵魂在您提供帮助之前就已经走了”
-
Perl 有更强大的工具 Regexp::Debugger - youtu.be/zcSFIUiMgAs?t=158