正则表达式只匹配一个单词

时间:2018-04-25 04:00:52

标签: python regex

我正在尝试匹配一行中的数字和所有单词后面的数字。我发现工作的正则表达式是'(90 [13])。*'。我在Mac上使用Python和Sublime。

在regex101.com上,它可以工作并匹配901和903以及每行后面的单词。但是,当我在我的代码编辑器中尝试时,它只匹配数字901和903,但不匹配后面的单词。

这是字符串

2008-11-08 06:32:46.354761500 26318 logging::logterse plugin: ` 89.223.216.72   apn-89-223-216-72.vodafone.hu   apn-89-223-216-72.vodafone.hu   <toshiter@donin.com>        rhsbl   901 Not supporting null originator (DSN)    msg denied before queued
2008-11-08 06:33:17.924158500 26331 logging::logterse plugin: ` 208.99.214.236  mx22.ecreditchoices7.com    mx22.ecreditchoices7.com    <moneydiet2@mx22.ecreditchoices7.com>       dnsbl   903 http://www.spamhaus.org/SBL/sbl.lasso?query=SBL69049    msg denied before queued
2008-11-08 06:34:53.318459500 26358 logging::logterse plugin: ` 84.58.57.150    dslb-084-058-057-150.pools.arcor-ip.net rpemgmu.arcor-ip.net    <sundered@ancientinc.com>       dnsbl   903 http://www.spamhaus.org/query/bl?ip=84.58.57.150    msg denied before queued
2008-11-08 06:35:41.724563500 26375 logging::logterse plugin: ` 58.126.113.198  Unknown [58.126.113.198]    <benny@surecom.com>     rhsbl   901 Not supporting null originator (DSN)    msg denied before queued
2008-11-08 06:37:31.730609500 26398 logging::logterse plugin: ` 87.103.146.91   pmsn.91.146.103.87.sable.dsl.krasnet.ru pmsn.91.146.103.87.sable.dsl.krasnet.ru <dwweem@wee.com>        dnsbl   903 http://www.spamhaus.org/query/bl?ip=87.103.146.91   msg denied before queued
2008-11-08 06:37:41.211401500 26409 logging::logterse plugin: ` 87.103.146.91   pmsn.91.146.103.87.sable.dsl.krasnet.ru pmsn.91.146.103.87.sable.dsl.krasnet.ru <dwtrupsm@trups.com>        dnsbl   903 http://www.spamhaus.org/query/bl?ip=87.103.146.91   msg denied before queued

表达式应匹配 &#34; 901不支持空发起者(DSN)msg在排队之前被拒绝&#34; 但只有匹配&#34; 901&#34;

with open('data/email_log.txt', 'r') as fh:
    email_log = fh.read()

print(re.findall('(90[13]).*', email_log))

1 个答案:

答案 0 :(得分:1)

在python中,使用re.findall会出现以下行为:

  

如果模式中存在一个或多个组,则返回组列表;

由于.*(在您的号码之后找到的任何内容)未包含在组中,因此它不会出现在最终结果中。完全删除组,或为您的号码后面的文本添加组:

s = """
2008-11-08 06:32:46.354761500 26318 logging::logterse plugin: ` 89.223.216.72   apn-89-223-216-72.vodafone.hu   apn-89-223-216-72.vodafone.hu   <toshiter@donin.com>        rhsbl   901 Not supporting null originator (DSN)    msg denied before queued
2008-11-08 06:33:17.924158500 26331 logging::logterse plugin: ` 208.99.214.236  mx22.ecreditchoices7.com    mx22.ecreditchoices7.com    <moneydiet2@mx22.ecreditchoices7.com>       dnsbl   903 http://www.spamhaus.org/SBL/sbl.lasso?query=SBL69049    msg denied before queued
2008-11-08 06:34:53.318459500 26358 logging::logterse plugin: ` 84.58.57.150    dslb-084-058-057-150.pools.arcor-ip.net rpemgmu.arcor-ip.net    <sundered@ancientinc.com>       dnsbl   903 http://www.spamhaus.org/query/bl?ip=84.58.57.150    msg denied before queued
2008-11-08 06:35:41.724563500 26375 logging::logterse plugin: ` 58.126.113.198  Unknown [58.126.113.198]    <benny@surecom.com>     rhsbl   901 Not supporting null originator (DSN)    msg denied before queued
2008-11-08 06:37:31.730609500 26398 logging::logterse plugin: ` 87.103.146.91   pmsn.91.146.103.87.sable.dsl.krasnet.ru pmsn.91.146.103.87.sable.dsl.krasnet.ru <dwweem@wee.com>        dnsbl   903 http://www.spamhaus.org/query/bl?ip=87.103.146.91   msg denied before queued
2008-11-08 06:37:41.211401500 26409 logging::logterse plugin: ` 87.103.146.91   pmsn.91.146.103.87.sable.dsl.krasnet.ru pmsn.91.146.103.87.sable.dsl.krasnet.ru <dwtrupsm@trups.com>        dnsbl   903 http://www.spamhaus.org/query/bl?ip=87.103.146.91   msg denied before queued
"""
import re
print(re.findall(r'(90[13])(.*)', s))

输出:

[('901', ' Not supporting null originator (DSN)    msg denied before queued'), ('903', ' http://www.spamhaus.org/SBL/sbl.lasso?query=SBL69049    msg denied before queued'), ('903', ' http://www.spamhaus.org/query/bl?ip=84.58.57.150    msg denied before queued'), ('901', ' Not supporting null originator (DSN)    msg denied before queued'), ('903', ' http://www.spamhaus.org/query/bl?ip=87.103.146.91   msg denied before queued'), ('903', ' http://www.spamhaus.org/query/bl?ip=87.103.146.91   msg denied before queued')]
相关问题