Reputation: 2039
I'm trying to split a multiline string in python using regex. My regex experience is limited.
The string has a structure like this:
asdf
foo 1
bar
barfoo
bar
foo 2
baz
baz
...
foo 20
bat
bat
I'm looking for an expression that will find multiple matches, as currently I only find everything or nothing. So the output should be something like:
["foo 1\nbarbarfoobar","foo 2 \nbaz\nbaz\n...",...]
As I understand it, I should look for a foo \d+
followed by some random stuff... ((\n|.*)?)*
foo \d+((\n|.*)?)*
I've researched about negative lookaheads, but inserting one did not help me.
foo \d+((\n|.*)?)*(?!foo)
I guess this could be solved by using only the built-in String
methods, but later on I'll have to do things like this alot, so I'm looking for a "simple" solution.
Upvotes: 1
Views: 127
Reputation: 67988
([\s\S]+?(?=(?:\nfoo \d+|$)))
You can try this with re.findall
.See demo.
https://regex101.com/r/mT0iE7/5
Upvotes: 1