繁体   English   中英

Python正则表达式在前面的子字符串匹配之后获取子字符串

[英]Python regex get substring after preceding substring match

文本没有空格,因此我根本无法拆分,也无法在字符串列表上使用索引。

我正在寻找的模式是:

check=

它后跟数字和编码的查询字符串项(apache日志文件),并且在文件的每一行中两次。 我想要的输出可以让我得到check=

例如,一行中的字符串如下所示:

11.249.222.103 - - [15/Aug/2016:13:17:56 -0600] "GET /next2/120005079807?check=37593467%2CCOB&check=37593378%2CUAP&box=match&submit=Next HTTP/1.1" 500 1633 "https://mvt.squaretwofinancial.com/newmed/?button=All&submit=Submit" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"

在这种情况下,我需要获取3759346737593378

请检查此代码。

import re

text = '''11.249.222.103 - - [15/Aug/2016:13:17:56 -0600] "GET /next2/120005079807?check=37593467%2CCOB&check=37593378%2CUAP&box=match&submit=Next HTTP/1.1" 500 1633 "https://mvt.squaretwofinancial.com/newmed/?button=All&submit=Submit" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"'''


for match in re.findall("check=(\d+)",text):
    print 'Found "%s"' % match

输出:

C:\Users\dinesh_pundkar\Desktop>python demo.py
Found "37593467"
Found "37593378"

几个URL寻求帮助:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM