Reputation: 131
Consider the following:
a;b{c;d;}e{f;}
How can I split this into three groups like so:
Group 1: a;
Group 2: b{c;d;}
Group 3: e{f;}
I'm learning regular expressions and I was wondering if I could include a if-then-else type logic into the expression.
What I have now:
(.*?{.*?})
This creates two groups like so:
Group 1: a;b{c;d;}
Group 2: e{f;}
which is not what I want as a; and b{c;d;} are merged.
Pseudo if-then-else:
Thanks.
Upvotes: 2
Views: 148
Reputation: 67968
(?<=;)(?![^{]*})|(?<=}(?!$))
You can use this to split with regex
module as re
does not support split at 0 width assertions
.
import regex
x="a;b{c;d;}e{f;}"
print regex.split(r"(?<=;)(?![^{]*})|(?<=}(?!$))",x,flags=regex.VERSION1)
Output:['a;', 'b{c;d;}', 'e{f;}']
See demo.
https://regex101.com/r/tJ2mW5/8#python
Upvotes: 1
Reputation: 174706
Use re.findall
>>> re.findall(r'[^;{]+;?(?:{[^}]*})?', 'a;b{c;d;}e{f;}')
['a;', 'b{c;d;}', 'e{f;}']
OR
This one is the more appropriate one.
>>> re.findall(r'[^;{]+;|[^;{]+(?:{[^}]*})?', 'a;b{c;d;}e{f;}')
['a;', 'b{c;d;}', 'e{f;}']
[^;{]+
negated character class which matches any char but not of ;
or {
one or more times.
|
OR
[^;{]+
any char but not of ; or {
followed by an
(?:{[^}]*})?
optional curly brace block.
Upvotes: 2