简体   繁体   English

正则表达式仅匹配一个词

[英]Regex only matching one word

I am trying to match a number and all the words that come after the number in a line. 我正在尝试匹配数字和一行中数字后面的所有单词。 The regular expression that I found to work was '(90[13]).*'. 我发现可以使用的正则表达式为'(90 [13])。*'。 I am using Python with Sublime on Mac. 我在Mac上将Python与Sublime结合使用。

On regex101.com, it works and matches 901 and 903 and the words that come after it on each line. 在regex101.com上,它可以工作并匹配每行上的901和903及其后面的单词。 However, when I try that in my code editor, it only matches the numbers 901 and 903 but not the words after. 但是,当我在代码编辑器中尝试该命令时,它仅匹配数字901和903,但不匹配其后的单词。

Here is the string 这是字符串

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

Expression should match "901 Not supporting null originator (DSN) msg denied before queued" but only matches "901" 表达式应匹配“ 901不支持在排队之前拒绝的空原始发件人(DSN)消息”,但仅匹配“ 901”

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

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

In python, using re.findall exhibits the following behavior: 在python中,使用re.findall表现出以下行为:

If one or more groups are present in the pattern, return a list of groups; 如果该模式中存在一个或多个组,则返回一个组列表;否则,返回一个列表。

Since .* (anything found after your numbers) is not contained in a group, it will not be in the final result. 由于.* (在您的号码之后找到的所有内容)未包含在组中,因此它不会出现在最终结果中。 Either remove the groups altogether, or add a group for the text following your number: 完全删除组,或在电话号码后的文本中添加组:

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

Output: 输出:

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

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

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