Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python RegEx Matching Newline

Tags:

python

regex

I have the following regular expression:

[0-9]{8}.*\n.*\n.*\n.*\n.*

Which I have tested in Expresso against the file I am working with and the match is sucessful.

I want to match the following:

  • Reference number 8 numbers long
  • Any character, any number of times
  • New Line
  • Any character, any number of times
  • New Line
  • Any character, any number of times
  • New Line
  • Any character, any number of times
  • New Line
  • Any character, any number of times

My python code is:

for m in re.findall('[0-9]{8}.*\n.*\n.*\n.*\n.*', l, re.DOTALL):
       print m

But no matches are produced, as said in Expresso there are 400+ matches which is what I would expect.

What I am missing here?

like image 408
humira Avatar asked Feb 03 '23 02:02

humira


1 Answers

Don't use re.DOTALL or the dot will match newlines, too. Also use raw strings (r"...") for regexes:

for m in re.findall(r'[0-9]{8}.*\n.*\n.*\n.*\n.*', l):
   print m

However, your version still should have worked (although very inefficiently) if you have read the entire file as binary into memory as one large string.

So the question is, are you reading the file like this:

with open("filename","rb") as myfile:
    mydata = myfile.read()
    for m in re.findall(r'[0-9]{8}.*\n.*\n.*\n.*\n.*', mydata):
        print m

Or are you working with single lines (for line in myfile: or myfile.readlines())? In that case, the regex can't work, of course.

like image 188
Tim Pietzcker Avatar answered Feb 05 '23 17:02

Tim Pietzcker