正则表达式:仅匹配括号外(以便文本不会在括号内拆分)? [英] Regex: match only outside parenthesis (so that the text isn't split within parenthesis)?

查看:20
本文介绍了正则表达式:仅匹配括号外(以便文本不会在括号内拆分)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的目标字符串:

I have a target string which looks like this:

"foo (foo, foofoo), bar (foobar), foo, bar (barbar, foo), bar, foo"

我想要:

["foo (foo, foofoo)", "bar (foobar)", "foo", "bar (barbar, foo)", "bar", "foo"]

通过在"、"处分割目标只在括号外.匹配括号外逗号的正则表达式是什么?就我而言,嵌套括号不会出现,我不必考虑它们.

by splitting the target at ", " only outside the parenthesis. What is the regex to match the commas outside the parenthesis? In my case, nested parenthesis do not appear and I don't have to consider them.

我个人使用 Python,但任何语言示例都可以.

I personally use Python but any language example is fine.

推荐答案

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

你可以用它来分割.看演示.这适用于你说没有嵌套的().

You can use this to split.See demo.This holds true as u said there are no nested ().

https://regex101.com/r/wV5bD0/1

这篇关于正则表达式:仅匹配括号外(以便文本不会在括号内拆分)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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