-3

I have a Python code that parses a text file and finds instances of the words within a tag @(my word). To do this I use, e.g.,:

import re

DEF_RE = r"(@\()(?P<text>.+?)\)"

sometext = "find the @(tagged text)."

m = re.search(DEF_RE, sometext)

print(m.group("text"))
tagged text

However, if the file contains the tagged text split over two lines the above method doesn't work, e.g.,

sometext = "find the @(tagged\ntext)."

m = re.search(DEF_RE, sometext)

print(m)
None

Is there are way to change my regular expression string to allow the text to be split over a line?

Matt Pitkin
  • 1,193
  • 9
  • 20

1 Answers1

-1

You should turn on the DOTALL flag when searching.

You can either replace re.search(DEF_RE, sometext) with re.search(DEF_RE, sometext, flags=re.DOTALL)

Proof

theX
  • 649
  • 3
  • 17