w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python regex search lines that end with a colon and all text after until next line that ends with colon

You could use the below regex which uses DOTALL modifier,

(?:^|
)(Test|Hello) *([^:]*):
(.*?)(?=
(?: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)(?:^|
)(Test|Hello) *([^:]*):
(.*?)(?=
(?:Test|Hello)|$)', s)
[('Test', '123', '
This is a blue car
'), ('Test', '', '
This car is not blue

This car is yellow
'), ('Hello', '', '
This is not a test')]




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.