ES87ME
ES87ME

Reputation: 57

Regex patterns doesnt match in python

I'm trying to match strings, in a text file. I'm using Python 3.4.3 and regex. I've tested the pattern in regex editor https://regex101.com/r/xZ7iL5/4#python. The Pattern works but when I tested with python It doesn't work: m returns nothing.

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

CODE


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)

Upvotes: 0

Views: 74

Answers (2)

co2y
co2y

Reputation: 179

the pattern contains three lines and you just match one line, so m returns nothing.

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)

Upvotes: 1

Lev Levitsky
Lev Levitsky

Reputation: 65791

If you check (print) the value of line, you'll see the problem.

line contains one line, and your pattern contains three. line will never contain two linebreaks in your example.

You need to rethink the parsing logic, either by manually reading three lines and concatenating them before looking for a match, or by dropping the multi-line regex and writing a parser that recognizes the lines separately.

Upvotes: 1

Related Questions