Regular expression negative lookbehind of non-fixed length

前端 未结 2 835
青春惊慌失措
青春惊慌失措 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...

提交回复
热议问题