As the document goes:
This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length.
So this will work, the intention is to match any ,
outside {}
, but not inside {}
:
In [188]:
re.compile("(?<!\{)\,.").findall('a1,a2,a3,a4,{,a6}')
Out[188]:
[',a', ',a', ',a', ',{']
this will work, on a slightly different query:
In [189]:
re.compile("(?<!\{a5)\,.").findall('a1,a2,a3,a4,{a5,a6}')
#or this: re.compile("(?<!\{..)\,.").findall('a1,a2,a3,a4,{a5,a6}')
Out[189]:
[',a', ',a', ',a', ',{']
In [190]:
But if the query is 'a1,a2,a3,a4,{_some_length_not_known_in_advance,a6}'
, according to the document the following won't work as intended:
In [190]:
re.compile("(?<![\{.*])\,.").findall('a1,a2,a3,a4,{a5,a6}')
Out[190]:
[',a', ',a', ',a', ',{', ',a']
Any alternative to achieve this? Is negative lookbehind the wrong approach?
Any reason this is how lookbehind was designed to do (only match strings of some fixed length) in the first place?