1

In reference to a previous question Python data extract from text file - script stops before expected data match

How can I capture a match and the previous two lines? I tried this but get:

unterminated subpattern at position 0 (line 1, column 1)

output = re.findall('(.*\r\n{2}random data.',f.read(), re.DOTALL)
arealhobo
  • 447
  • 1
  • 6
  • 17
  • What do you want to match? Repeated groups of two lines, like `line1\n line2\n line1\n line2\n`? Or lines with some spefific text? – mrzasa Feb 22 '18 at 09:42

1 Answers1

4

You may use

re.findall(r'(?:.*\r?\n){2}.*random data.*', s)

Note you can't use re.DOTALL or .* will match up to the end of the input and you will only get the last occurrence.

See the Python demo

Pattern details

  • (?:.*\r?\n){2} - 2 occurrences of a sequence of
    • .* - any 0+ chars other than line break chars, as many as possible (a line)
    • \r?\n - a line ending (CRLF or LF)
  • .*random data.* - a line containing random data substring.

See the regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563