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

2024-05-19 14:13:57 发布

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

我试图匹配一个数字和一行中数字后面的所有单词。我发现有效的正则表达式是“(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

表达式应该匹配 “901不支持空原始发件人(DSN)消息在排队前被拒绝” 但只匹配“901”

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

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

Tags: orgipcomhttploggingwwwmsgquery
1条回答
网友
1楼 · 发布于 2024-05-19 14:13:57

在python中,使用^{}表现出以下行为:

If one or more groups are present in the pattern, return a list of groups;

由于.*(在数字之后找到的任何内容)不包含在组中,因此它不会出现在最终结果中。请完全删除这些组,或为编号后的文本添加一个组:

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')]

相关问题 更多 >