I have a text block as below:
group 1
name A
name B
name C
group 2
name X
name Y
name Z
group 3
name I
name II
name III
It's a nest groups, (group 1, group 2, group 3,...), and each group contains sub group, (name x, name y,name z,...). How can I use regex to match this nested groups and sub groups with python?
I try below find all pattern to match outer groups fine, but how to add more code to match sub groups as well?
import re
content = """
group 1
name A
name B
name C
group 2
name X
name Y
name Z
group 3
name I
name II
name III
"""
m = re.findall("(group \d+).*?name",content,re.DOTALL)
if m:
for e in m:
print(e)
output:
: group 1
: group 2
: group 3
expected output:
: group 1
: name A
: name B
: name C
: group 2
: name X
: name Y
: name Z
: group 3
: name I
: name II
: name III
Note, groups and sub groups number may not the same!