Skip to content Skip to sidebar Skip to footer

Regex To Match Only Comma's But Not Inside Multiple Parentheses

Okay, i have this case where comma are inside parenthesis, I want to match the commas that are only outside the parenthesis. Input : color-stop(50%,rgb(0,0,0)), color-stop(51%,rgb(

Solution 1:

Here is the regex which works perfectly for your input.

,(?![^()]*(?:\([^()]*\))?\))

DEMO

Explanation:

,                        ','
(?!                     negative look ahead, to see if there isnot:
  [^()]*anycharacterexcept: '(', ')' (0or
                           more times)
  (?:                      group, but do not capture (optional):
    \(                       '('
    [^()]*anycharacterexcept: '(', ')' (0or
                             more times)
    \)                       ')'
  )?                       endofgrouping, ? after the non-capturing group makes the
                           whole non-capturing groupas optional.
  \)                       ')'
)                        endof look-ahead

Limitations:

This regex works based on the assumption that parentheses will not be nested at a depth greater than 2, i.e. paren within paren. It could also fail if unbalanced, escaped, or quoted parentheses occur in the input, because it relies on the assumption that each closing paren corresponds to an opening paren and vice versa.

Post a Comment for "Regex To Match Only Comma's But Not Inside Multiple Parentheses"