【问题标题】:Regex patterns doesnt match in python正则表达式模式在python中不匹配
【发布时间】:2016-08-29 16:07:13
【问题描述】:

我正在尝试匹配文本文件中的字符串。我正在使用 Python 3.4.3 和正则表达式。我已经在正则表达式编辑器https://regex101.com/r/xZ7iL5/4#python 中测试了该模式。模式有效,但是当我用 python 测试时它不起作用:m 什么都不返回。

Boot_info.txt


boot.1.type=HARDDISK
boot.1.group=+Hard Drive 
boot.1.name=+ST380215AS
boot.2.type=HARDDISK
boot.2.group=+Hard Drive 
boot.2.name=+ST9250315AS
boot.3.type=USBKEY
boot.3.group=+Unknown Device 
boot.3.name=+U1-KingstonDataTraveler G3 1.00

代码


Boot_info = "Boot_info.txt"
def Get_boot ():
   global Boot_info 
   Indexes = []
   Names = []
   Types = []
   Form = r"boot(.\d.)type=(.*)\n.*\nboot.\d.name=(.*)"
   with open("Boot_info.txt") as Boot_info:
    p = re.compile(Form) 
    for line in Boot_info:
        m = p.match(line)
        if m != None:
           Index=m.group(1)
           Type=m.group(2)
           Name=m.group(3)
           logging.info("The connected device Index is:%s",Index)
           logging.info("The connected device Type is:%s",Type)
           logging.info("The connected device Name is :%s",Name)
           Indexes.append(Index)
           Types.append(Type)
           Names.append(Name)
           logging.info("The connected devices Types in order are :%s", Types)
           logging.info("The connected devices Names in order are :%s", Names)
        else:
           logging.error("Regex failed!! check the Pattern")
   return (len(Indexes),Types,Names)

import re
L,typ,Nm1=Get_boot()
print('Nm1:',Nm1) 
print('Length:',L)

【问题讨论】:

    标签: regex python-3.x


    【解决方案1】:

    如果您检查(打印)line 的值,您会发现问题。

    line 包含一行,而您的模式包含三行。 line 在您的示例中永远不会包含两个换行符。

    您需要重新考虑解析逻辑,要么在查找​​匹配项之前手动读取三行并将它们连接起来,要么删除多行正则表达式并编写一个单独识别行的解析器。

    【讨论】:

    • 通过删除多行正则表达式,我可以使用两种形式,但我不知道如何以单一模式进行。你有什么想法吗?
    • @MomoEssassi 我不确定我理解你所说的“两种形式”是什么意思。您可以将任何行与boot\.(\d+)\.(\w+)=(.*) 之类的内容匹配,然后根据第二组的值做出决定。
    【解决方案2】:

    模式包含三行,你只匹配一行,所以 m 什么也不返回。

    import logging
    
    Boot_info = "Boot_info.txt"
    
    
    def Get_boot():
        global Boot_info
        Indexes = []
        Names = []
        Types = []
        Form = r"boot(.\d.)type=(.*)\n.*\nboot.\d.name=(.*)"
        with open("Boot_info.txt") as Boot_info:
            p = re.compile(Form)
            for line in Boot_info:
                lines = ""
                lines += line
                lines += Boot_info.readline()
                lines += Boot_info.readline()
    
                m = p.search(lines)
    
                if m != None:
                    Index = m.group(1)
                    Type = m.group(2)
                    Name = m.group(3)
                    logging.info("The connected device Index is:%s", Index)
                    logging.info("The connected device Type is:%s", Type)
                    logging.info("The connected device Name is :%s", Name)
                    Indexes.append(Index)
                    Types.append(Type)
                    Names.append(Name)
                    logging.info("The connected devices Types in order are :%s", Types)
                    logging.info("The connected devices Names in order are :%s", Names)
                else:
                    logging.error("Regex failed!! check the Pattern")
    
        return (len(Indexes), Types, Names)
    
    
    import re
    
    L, typ, Nm1 = Get_boot()
    print('Nm1:', Nm1)
    print('Length:', L)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多