Reputation: 179
I'm trying to split something like the following string
s = '1 2 3 {test 0, test 0} {test 0 test 0}'
what I'm trying to get is
['1', '2', '3', '{test 0, test 0}', '{test 0, test 0}']
or
['1', '2', '3', 'test 0, test 0', 'test 0, test 0']
Could someone please help me?
Thanks
Upvotes: 1
Views: 3238
Reputation: 67968
\s(?![^{]*})
Split by this.See demo.
https://regex101.com/r/vN3sH3/4
re.split(r"\s(?![^{]*})",s)
Or
print re.split(r"\s(?![^{]*})|{|}",s)
If you dont want {}
as well.
Explanation:\s space
(?![^{]*})
negative lookahead stating after space there should not be }
which has no {
before it.
So this way {test 0 test 0}
the space after 0
will not be considered as it has }
ahead wihtout {
.
Upvotes: 3