Given a list of references, I want to split each of those.
[1] Some text, and some more
and even some more
[2] Some more text
I want to get each of those references as a list of strings like below:
['[1] Some text, and some more\n and even some more', '[2] Some more text ']
When I do it on https://regex101.com/r/YPWt9Z/1, it marks all the matches correctly. But when I do the same in python, I only get the first match:
with open('ref.txt', 'r') as file:
data=file.read()
lines = re.findall(r'^\[[^\[]*', data)
for x in lines:
print(x) #x just prints the first match, not all
How do I get all the matches?
>Solution :
To have findall() match each line in the string separately, you need to call it with a flag:
lines = re.findall(r'^\[[^\[]*', data, re.MULTILINE)
Alternatively use re.split():
lines = re.split(r'\n(\[\d+\])')