As the document goes:
This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match
Any alternative to achieve this?
Yes. There is a a brilliantly simple technique, and this situation is very similar to "regex-match a pattern unless..."
Here's your simple regex:
{[^}]*}|(,)
The left side of the alternation |
matches complete { brackets }
tags. We will ignore these matches. The right side matches and captures commas to Group 1, and we know they are the right commas because they were not matched by the expression on the left.
Here is a demo that performs several tasks, so you can pick and choose (see the output at the bottom of the demo):
SplitHere
so we can perform task 4...Reference
How to match (or replace) a pattern except in situations s1, s2, s3...
Instead of using Negative Lookbehind, you can use Negative Lookahead with balanced braces.
,(?![^{]*\})
For example:
>>> re.findall(r',..(?![^{]*\})', 'a1,a2,a3,a4,{_some_unknown_length,a5,a6,a7}')
[',a2', ',a3', ',a4']