Python regex search strings that end with a colon and all text after the next line that ends with a colon
I have the following text:
Test 123:
This is a blue car
Test:
This car is not blue
This car is yellow
Hello:
This is not a test
I want to concatenate a regex that finds all elements starting with Test
or Hello
and preceded by a colon and optionally a tree digit number and returns all content after that until the next line that fits that same description. So, for the text above, the return expression findall returns an array:
[("Test", "123", "\nThis is a blue car\n"),
("Test", "", "\nThis car is not blue\n\nThis car is yellow\n"),
("Hello", "", "\nThis is not a test")]
So far I got this:
r = re.findall(r'^(Test|Hello) *([^:]*):$', test, re.MULTILINE)
It matches each line as described, but I'm not sure how to grab the content up to the next line that ends with a colon. Any ideas?
+3
source to share
2 answers
You can use the following regular expression which uses the DOTALL modifier,
(?:^|\n)(Test|Hello) *([^:]*):\n(.*?)(?=\n(?:Test|Hello)|$)
>>> import re
>>> s = """Test 123:
...
... This is a blue car
...
... Test:
...
... This car is not blue
...
... This car is yellow
...
... Hello:
...
... This is not a test"""
>>> re.findall(r'(?s)(?:^|\n)(Test|Hello) *([^:]*):\n(.*?)(?=\n(?:Test|Hello)|$)', s)
[('Test', '123', '\nThis is a blue car\n'), ('Test', '', '\nThis car is not blue\n\nThis car is yellow\n'), ('Hello', '', '\nThis is not a test')]
+5
source to share
import re
p = re.compile(ur'(Test|Hello)\s*([^:]*):\n(\n.*?)(?=Test[^:]*:|Hello[^:]*:|$)', re.DOTALL | re.IGNORECASE)
test_str = u"Test 123:\n\nThis is a blue car\n\nTest:\n\nThis car is not blue\n\nThis car is yellow\n\nHello:\n\nThis is not a test"
re.findall(p, test_str)
You can try this. See demo.
0
source to share