【问题标题】:regex for filtering word based on length or by excluding a word in Python正则表达式,用于根据长度过滤单词或通过排除 Python 中的单词
【发布时间】:2018-06-16 22:21:30
【问题描述】:

我一直在尝试解决这个问题,但由于我是 regex 的新手,所以我一直无法解决。我需要选择一些 telnet 输出的正确行,如下所示:

systemstatus get resume    # line to exclude
systemstatus get idle      # line to filter
systemstatus get talking   # line to filter
systemstatus get ringing   # line to filter
systemstatus get outgoing  # line to filter
systemstatus get sleeping  # line to filter

如您所见,我需要排除带有简历的人并选择所有其他人。所以我知道我可以按长度过滤,但我只知道如何按大于某物的长度过滤,而不是按很多长度过滤。例如:"systemstatus get \w{7,}" 将排除 resume 行,但也排除 idle 行。所以实际上我需要一些过滤长度为 4、7 和 8 的东西。

有人知道怎么做吗?

注意:由于 telnet 库,这必须在正则表达式中完成。

注意2:由于是远程登录,我必须在出现systemstatus get resume 时继续阅读(这就是我所说的“排除”),而不是像systemstatus get idle 出现时那样停止。所以过滤“ systemstatus get WHATEVER”然后排除“resume”会在“resume”进入时停止阅读。我正在使用telnet lib的telnet.expect([], timeout)。

【问题讨论】:

  • 在systemstatus get WHATEVER 中获得... somehow "WHATEVER" 并根据它进行过滤不是更好吗?

标签: python regex telnet


【解决方案1】:

虽然正则表达式很强大,但这里并不真正需要它们,只需拆分、应用和组合即可:

text = """
systemstatus get resume    # line to exclude
systemstatus get idle      # line to filter
systemstatus get talking   # line to filter
systemstatus get ringing   # line to filter
systemstatus get outgoing  # line to filter
systemstatus get sleeping  # line to filter
"""

lines = "\n".join([line for line in text.split("\n") 
                  if line and not "resume" in line])
print(lines)

这会产生

systemstatus get idle      # line to filter
systemstatus get talking   # line to filter
systemstatus get ringing   # line to filter
systemstatus get outgoing  # line to filter
systemstatus get sleeping  # line to filter

除非您碰巧没有像systemstatusresumesystem get idle 这样的文本(意思是resume 没有任何单词边界),否则不需要正则表达式引擎的开销。


定时不同的方法(每个 100k)产生
print(timeit.timeit(noregex, number=10**5))
# 0.28622116599945 s

print(timeit.timeit(regex, number=10**5))
# 0.5753898609982571 s

所以非正则表达式解决方案只需要一半时间。

【讨论】:

    【解决方案2】:

    选项 1
    使用re.MULTILINE 开关呼叫re.findall。

    matches = re.findall(r"systemstatus get \b(?:\w{4}|\w{7,8})\b", t, re.M)
    

    它将每个匹配项作为字符串列表返回。

    正则表达式详细信息

    systemstatus get    # literals
    \b                  # word boundary
    (?:                 # non-capturing group
    \w{4}               # find a word of size 4 
    |                   # regex OR pipe
    \w{7,8}             # find a word of size 7 or 8
    )
    \b
    

    由于您的要求,我们在这里按字长匹配 -

    我需要过滤长度为 4、7 和 8 的东西。


    选项 2
    将多行字符串拆分为单独的行,遍历每一行并在每一行上调用 re.match -

    matches = []
    
    for line in t.splitlines():
        if re.match(r"systemstatus get \b(?:\w{4}|\w{7,8})\b", line):
            matches.append(line)  
    

    【讨论】:

    • 按令牌计数匹配非常脆弱(大部分时间)。
    • @heemayl 你的意思是字数?这正是 OP 想要的。
    • 是的。等到更改为 anything :)
    • @heemayl 我明白你在说什么。但是根据 OP 的要求,这个正则表达式应该足够灵活。
    • D'oh...你找到我了 :)
    【解决方案3】:

    使用零宽度负前瞻 ((?!resume(?:\s|$))) 以确保 resume 不会出现在 systemstatus get 之后:

    ^systemstatus get (?!resume(?:\s|$)).*$
    

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 2021-04-20
      • 2017-03-13
      相关资源
      最近更新 更多