简体   繁体   中英

Python regex search lines that end with a colon and all text after until next line that ends with 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 put together a regex that finds all items that start with a Test or a Hello and precede a colon, and optionally a tree digit number, and return all content after that until the next line that fits that same description. So for above text, the findall regex would return an array of:

[("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 according to the description but I'm unsure how to capture the content until the next line that ends with a colon. Any ideas?

You could use the below regex which uses DOTALL modifier,

(?:^|\n)(Test|Hello) *([^:]*):\n(.*?)(?=\n(?:Test|Hello)|$)

DEMO

>>> 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')]
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.

http://regex101.com/r/eM1xP0/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM