正则表达式只匹配逗号但不在多个括号内 [英] Regex to match only comma's but not inside multiple parentheses

查看:697
本文介绍了正则表达式只匹配逗号但不在多个括号内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有这种情况,其中逗号在括号内,我想匹配仅在括号内的逗号。

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(0,0,0)), color-stop(100%,rgb(0,0,0)))

Output(i'm looking for):color-stop(50%,rgb(0,0,0))**,** color-stop(51%,rgb(0,0,0))**,** color-stop(100%,rgb(0,0,0)))

这是我的正则表达式: -

And this is my regex:-

/(?![^(]*\)),/g

可悲的是,当有一个时,它不起作用多个括号:(

The sad part is, it doesn't work when there is a multiple parenthesis :(

正则表达式

推荐答案

这是适用于您输入的正则表达式。

Here is the regex which works perfectly for your input.

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

DEMO

说明:

,                        ','
(?!                      look ahead to see if there is not:
  [^()]*                   any character except: '(', ')' (0 or
                           more times)
  (?:                      group, but do not capture (optional):
    \(                       '('
    [^()]*                   any character except: '(', ')' (0 or
                             more times)
    \)                       ')'
  )?                       end of grouping, ? after the non-capturing group makes the
                           whole non-capturing group as optional.
  \)                       ')'
)                        end of look-ahead

这篇关于正则表达式只匹配逗号但不在多个括号内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆