Regular expression negative lookbehind of non-fixed length

前端 未结 2 808
青春惊慌失措
青春惊慌失措 2021-01-12 10:18

As the document goes:

This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match

相关标签:
2条回答
  • 2021-01-12 10:48

    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):

    1. Count the commas you want to match (not those between braces)
    2. Show the matches (commas... duh)
    3. Replace the right commas. Here we replace with SplitHere so we can perform task 4...
    4. Split on the commas, and display the split strings

    Reference

    How to match (or replace) a pattern except in situations s1, s2, s3...

    0 讨论(0)
  • 2021-01-12 10:52

    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']
    
    0 讨论(0)
提交回复
热议问题