Reputation: 95
I'm new to Python and I'm still learning about regular expressions. I want to get a paragraph between two key words; something like this:
Beginning of the text
Keyword1
Paragraph content
Keyword2
End of the text
How could I do it? I've tried this:
(?i)Keyword1(.*?)((?i)Keyword2Variant1|(?i)Keyword2Variant2)
But it doesn't really work.
Upvotes: 0
Views: 224
Reputation: 12182
You need to make sure the single dot also matches newlines: use the re.DOTALL
flag.
Slightly modified from your (non-workable) example:
text = """
Keyword1
Paragraph content
Keyword2Variant2
"""
import re
pattern = 'Keyword1(.*?)(Keyword2Variant1|Keyword2Variant2)'
match = re.search(pattern, text, re.IGNORECASE|re.DOTALL)
print(match.group(1))
yields
Paragraph content
(two blank lines before and after, but the rendering makes them hardly visible.)
Upvotes: 3