Python解析模块搜索

2024-04-19 13:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python解析模块。 我在一个字符串中搜索。 搜索“AB Number”,然后是多个空格,然后是格式为1-123456789的数字。你知道吗

所以“AB号1-7272882920”

如果我用这个,它会找到它,但它会分裂成多个匹配项:-你知道吗

search('AB Number{:s}{:d}{:D}{:d}',plain_text_body)

如果我用这个,它只匹配第一个数字,我不能找出为什么它不匹配

search('AB Number{:s}{\d+\D\d+}',plain_text_body)

另外,我认为在捕获的模式之外使用regex也是可能的,但是我也不能做到这一点,例如,捕获多个空格应该是:-

search('AB Number\s+{\d+\D\d+}',plain_text_body)

但它不起作用。 你知道吗?你知道吗


Tags: 模块字符串textnumbersearchab格式模式
1条回答
网友
1楼 · 发布于 2024-04-19 13:16:33

试试看。你知道吗

import re

pattern = r'^(AB Number).+(\d\-\d{1,})$'
text = 'AB Number sdf sfd  1-727288292920'

for m in re.match(pattern, text).groups(): 
    print(repr(m))

输出

... 
'AB Number'
'1-727288292920'

相关问题 更多 >