【问题标题】:Detecting a specific string with regex in python在python中使用正则表达式检测特定字符串
【发布时间】:2021-04-08 17:33:45
【问题描述】:

我有一个具有以下字符串结构的 csv 文件:

Modem Switch (MMA-213-MML-NW-Match-New Year)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New1 Month)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New Month 2)(32655)(12532)
....

我想得到匹配之后的任何字符串: 例如预期的结果共享如下:

New Year
New Month
New1 Month
New Month 2

使用以下代码无法获取我的相对字符串:

matches = re.findall(r'(Match-)(\w+)', inp, flags=re.I)

【问题讨论】:

  • 使用re.findall(r'\bMatch-([^)]+)', inp, flags=re.I)

标签: python python-3.x regex python-2.7 re


【解决方案1】:

这行得通:

import re
inp = "Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)"
matches = re.findall(r'Match-(.+?)\)', inp, flags=re.I)

给予

['New Month']

【讨论】:

    【解决方案2】:

    您还可以匹配所有后面的单词字符,中间有空格,并使用单个捕获组。

    \bMatch-(\w+(?:[^\S\r\n]+\w+)*)
    

    Regex demo | Python demo

    import re
     
    regex = r"\bMatch-(\w+(?:[^\S\r\n]+\w+)*)"
     
    s = ("Modem Switch (MMA-213-MML-NW-Match-New Year)(32655)(12532)\n"
        "Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)\n"
        "Modem Switch3 (MMA-1234-431-NW-Match-New1 Month)(32655)(12532)\n"
        "Modem Switch3 (MMA-1234-431-NW-Match-New Month 2)(32655)(12532)")
     
    print(re.findall(regex, s))
    

    输出

    ['New Year', 'New Month', 'New1 Month', 'New Month 2']
    

    或者要在括号之间匹配Match- 之后的所有字符,您可以使用negated character class 匹配除() 之外的任何字符

    \([^()]*\bMatch-([^()]+)\)
    

    Regex demo

    【讨论】:

      【解决方案3】:

      使用

      re.findall(r'(?<=Match-)[^()]+', inp, flags=re.I)
      

      regex proof

      说明

      --------------------------------------------------------------------------------
        (?<=                     look behind to see if there is:
      --------------------------------------------------------------------------------
          Match-                   'Match-'
      --------------------------------------------------------------------------------
        )                        end of look-behind
      --------------------------------------------------------------------------------
        [^()]+                   any character except: '(', ')' (1 or more
                                 times (matching the most amount possible))
      

      Python code:

      import re
      inp = """Modem Switch (MMA-213-MML-NW-Match-New Year)(32655)(12532)
      Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)
      Modem Switch3 (MMA-1234-431-NW-Match-New1 Month)(32655)(12532)
      Modem Switch3 (MMA-1234-431-NW-Match-New Month 2)(32655)(12532)"""
      print(re.findall(r'(?<=Match-)[^()]+', inp, flags=re.I))
      

      结果['New Year', 'New Month', 'New1 Month', 'New Month 2']

      【讨论】:

        猜你喜欢
        • 2020-05-07
        • 1970-01-01
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        • 2014-02-08
        相关资源
        最近更新 更多