I
I
irtf2020-05-06 00:44:27
Python
irtf, 2020-05-06 00:44:27

How to write a regular expression to work with a multiline field?

Hello.

There is this text:

anyname 5 { 
строка 1
строка 2
}

We need such a regular expression that will display data in such a list:
match[1] = '5'
match[2] = 'Строка 1'
match[3] = 'Строка 2'

So far I've settled on this solution:
reply (\d) { 
(.*)
(.*)
}

But there are disadvantages:
1. A template with several lines looks very stupid
2. There can be an infinite number of lines inside the brackets

Please help :c

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2020-05-06
@irtf

a = re.search(r"(?s)(\d+)\s*{\s*(.*?)}", text)
m = [a.group(1)] + a.group(2).splitlines()
print (m)

X
xmoonlight, 2020-05-06
@xmoonlight

/(.+(\d+))/m
Full match - string ("word digit", "many words digit").
The 2nd group in each match is the number after the word in each line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question