Regular expression of negative distortion of fixed length

How the document goes:

This is called a negative lookbehind statement. Like the positive lookbehind statements, the contained pattern should only match strings of some fixed length .

So this will work, the intent should match anyone ,outside {}, but not inside {}:

In [188]:

re.compile("(?<!\{)\,.").findall('a1,a2,a3,a4,{,a6}')
Out[188]:
[',a', ',a', ',a', ',{']

this will work, in a slightly different request:

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 request 'a1,a2,a3,a4,{_some_length_not_known_in_advance,a6}', according to the document, the following actions will not 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 a negative approach wrong?

, lookbehind ( )?

+4
2

?

. , "regex-match pattern, ..."

:

{[^}]*}|(,)

| { brackets }. . 1, , , .

, , (. ):

  • , ( )
  • (... duh)
  • . SplitHere, 4...

( ) , s1, s2, s3...

+4

Negative Lookbehind Negative Lookahead .

,(?![^{]*\})

:

>>> re.findall(r',..(?![^{]*\})', 'a1,a2,a3,a4,{_some_unknown_length,a5,a6,a7}')
[',a2', ',a3', ',a4']
+3

Source: https://habr.com/ru/post/1543644/


All Articles