Python does not recognise valid RegEx entry

Viewed 52

I'm trying to write a Python script to parse through a log file. Script core is borrowed from pythonic ways.

import re
log_file_path = r"O:\ZTK log file parser\2 Parsing Log\JP"
regex = '8355371640847825590'
match_list = []

with open(log_file_path, "r") as file:
    for line in file:
        for match in re.finditer(regex, line, re.S):
            match_text = match.group()
            match_list.append(match_text)
print(match_list)  # work in progress

Above example works well when parsing for plain string values. But when I try to insert regex variable:

regex = '((.*\n){2}).*8355371640847825590'

It always returns an empty list.

What bothers me is that this expression works really well in test environments suchas as https://regex101.com/. Each value is correctly matched. Unfortunately, I cannot replicate this in Python.

I'd be grateful if you could assist me.

1 Answers

You need to read the whole file into a single variable if you want your pattern to match across line breaks. Besides, you may explicitly let the regex engine know that you need to only start matching from the start of a line,

(?m)^(?:.*\n){2}.*8355371640847825590

See the regex demo.

Details

  • (?m) - (the inline re.M / re.MULTILINE modifier) ^ will now match start of line positions
  • ^ - start of a line
  • (?:.*\n){2} - two lines with line breaks
  • .*8355371640847825590 - any 0 or more chars other than line break chars as many as possible and then 8355371640847825590

Python demo:

import re
log_file_path = r"O:\ZTK log file parser\2 Parsing Log\JP"
regex = '(?m)^(?:(?:.*\n){2}).*8355371640847825590'
match_list = []

with open(log_file_path, "r") as file:
    match_list = re.findall(regex, file.read())

print(match_list)
Related