Search code examples
regexgoogle-docsregex-lookaroundslookbehind

Regex Lookahead/Lookbehind if more than one occurance


I have string formulas like this:

?{a,b,c,d}

It can be can be embedded like this:

?{a,b,c,?{x,y,z}}

or this is the same:

?{a,b,c,
    ?{x,y,z}
}

So I have to find those commas, what are in the second and greather "level" brackets. In the example below I marked the "levels" where I have to find all commas:

?{a,b,c,
    ?{x,y,          <--Those
        ?{1,2,3}    <--Those
    }
}

I've tried with lookahead and lookbehind, but I'm totally confused now :/

Here is my latest working try, but it is not good at all: OnlineRegex

Update: To avoid misunderstanding, I don't want to count the commas. I'd like to get groups of commas to replace them.

The condition is find the commas where more than one "open tags" before it like this: ?{

.. without closing tag like this: }

Examlpe.: In this case I have not replace any commas:

?{1,2,3} ?{a,b,c}

But in this case I have to replace commas between a b c

?{1,2,3,?{a,b,c}}

Solution

  • For the examples which you have provided, the following regex works(gives the desired output as mentioned by you):

    (?<!^\?{[^{}]*),(?=[\s\S]*(?:\s*}){2,})
    

    For String ?{a,b,c,d}, see Demo1 No Match


    For String, ?{a,b,c,?{x,y,z}}, see Demo2 Match successful


    For String,

    ?{a,b,c,
        ?{x,y,z}
    }
    

    see Demo3 Match Successful


    For String,

    ?{a,b,c,
        ?{x,y,          
            ?{1,2,3}   
        }
    }
    

    see Demo4 Match Successful


    For String ?{1,2,3} ?{a,b,c} ?{1,2,3} ?{a,b,c}, see Demo5 No Match


    Explanation:

    • (?<!^\?{[^{}]*), - negative lookbehind to discard the 1st level commas. The logic applied here is it should not match the comma which is preceded by start of the string followed by ?{ followed by 0+ occurrences of any character except { or }
    • (?=[\s\S]*(?:\s*}){2,}) - The comma matched above must be followed by atleast 2 occurrences of }(consecutive or having only whitespaces between them)