gpt4 book ai didi

python - 如何在 Python 中调试正则表达式?

转载 作者:太空狗 更新时间:2023-10-29 16:54:56 25 4
gpt4 key购买 nike

有没有办法在 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+)"

最佳答案


>>> p = re.compile('.*', re.DEBUG)
max_repeat 0 65535
any None
>>>

regex '|' operator vs separate runs for each sub-expression

关于python - 如何在 Python 中调试正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/606350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com