本文介绍了如何调试python中的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Python中调试正则表达式?而且我不是指尝试和尝试,直到他们工作的过程:)



编辑:这里是如何在perl中调试正则表达式:

  
使用re'debug';

我的$ str =GET http://some-site.com HTTP / 1.1;
if($ str =〜/ get\s +(\S +)/ i){
printMATCH:$ 1\\\
;
}

上面的代码在我的计算机上生成以下输出:

  
编译RExget\s +(\S +)
最终程序:
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
匹配RExget\s +(\S +反对GET http://some-site.com HTTP / 1.1
匹配stclass EXACTF与GET http://some-site.com HTTP / 1.1(33个字符)
0 | 1:EXACTF(3)
3 | 3:PLUS(5)
SPACE可以匹配1次,从2147483647 ...
4 | 5:OPEN1(7)
4 | 7:PLUS(9)
NSPACE可以匹配20次的2147483647 ...
24 | 9:CLOSE1(11)
24 | 11:END(0)
匹配成功!
MATCH:http://some-site.com
释放REx:get\s +(\S +)

pre>

解决方案
 

>>> p = re。编译('。*',re.DEBUG)
max_repeat 0 65535
any无
>>>


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 :)

EDIT: here is how regexes can be debugged in perl :


use re 'debug';

my $str = "GET http://some-site.com HTTP/1.1";
if($str =~/get\s+(\S+)/i) {
    print "MATCH:$1\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+)"

解决方案

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

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

这篇关于如何调试python中的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:10