0

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)

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

by dropping the multiline regex I can use two forms but I don't no how to do it in a single pattern. Have u any ideas ?
@MomoEssassi I'm not sure I understand what you mean by "two forms". You can match any of the lines with something like boot\.(\d+)\.(\w+)=(.*) and then make decisions based on the value of the 2nd group.
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.