Gowtham Saminathan
Gowtham Saminathan

Reputation: 45

Python regex multi line matching

I have to match multiple lines in python.

group one start
line 1 data
group end
group two start
group two data
group end

on the above string how to get below output

[group one start \n line 1 data \n group end, group two start \n group two data \n group end]

I have tried below code but not working

import re 

re.findall(r'group.*start.*group end',re.MULTILINE | re.DOTALL)

for info in data:
   print info

Upvotes: 0

Views: 253

Answers (3)

Gowtham Saminathan
Gowtham Saminathan

Reputation: 45

Below code working for me

a = """group one start
line 1 data
group end
group two start
group two data
group end
"""
all_m = re.findall(r'group.*?start.*?group end',a,re.DOTALL)
for m in all_m:
    print(m)
    print("**********")

Output

group one start
line 1 data
group end
*************
group two start
group two data
group end
*************

Upvotes: 0

Sunitha
Sunitha

Reputation: 12005

You can just split the text based on the pattern group end, but without capturing it using a look-behind

>>> import re
>>> text_data = """group one start
... line 1 data
... group end
... group two start
... group two data
... group end"""
>>> 
>>> re.split(r'(?<=group end)\n', text_data)
['group one start\nline 1 data\ngroup end', 'group two start\ngroup two data\ngroup end']

Upvotes: 0

Emma
Emma

Reputation: 27723

Maybe, an expression somewhat similar to:

\bgroup [\s\S]*? start\b[\s\S]*?\bgroup end\b

DEMO 1

or:

\bgroup .*? start\b.*?\bgroup end\b

DEMO 2

with a DOTALL flag might be working here.

Test with DOTALL:

import re

regex = r"\bgroup .*? start\b.*?\bgroup end\b"

test_str = """
group one start
line 1 data
group end
group two start
group two data
group end
"""

print(re.findall(regex, test_str, re.DOTALL))

Test without DOTALL:

import re

regex = r"(\bgroup [\s\S]*? start\b[\s\S]*?\bgroup end\b)"

test_str = """
group one start
line 1 data
group end
group two start
group two data
group end

"""


print(re.findall(regex, test_str))

Output

['group one start\nline 1 data\ngroup end', 'group two start\ngroup two data\ngroup end']

The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

Upvotes: 2

Related Questions