如何用不在括号内的逗号分隔? [英] How to split by commas that are not within parentheses?

查看:44
本文介绍了如何用不在括号内的逗号分隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的字符串,其中的项目用逗号分隔,但在带有括号内容的项目中也可能有逗号:

(抱歉,忘了提及某些项目可能没有括号内容)

水、二氧化钛 (CI 77897)、黑色 2 (CI 77266)、氧化铁 (CI 77491、77492、77499)、群青 (CI 77007)"

如何只用不在括号内的逗号分割字符串?即:

[水"、二氧化钛 (CI 77897)"、黑色 2 (CI 77266)"、氧化铁(CI 77491、77492、77499)"、群青 (CI 77007)"]

我想我必须使用正则表达式,也许是这样的:

([(]?)(.*?)([)]?)(,|$)

但我仍在努力让它发挥作用.

解决方案

使用 否定前瞻 匹配所有不在括号内的逗号.根据匹配的逗号拆分输入字符串将为您提供所需的输出.

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

演示

<预><代码>>>>进口重新>>>s = 水、二氧化钛 (CI 77897)、黑色 2 (CI 77266)、氧化铁 (CI 77491、77492、77499)、群青 (CI 77007)">>>re.split(r',\s*(?![^()]*\))', s)['水'、'二氧化钛 (CI 77897)'、'黑色 2 (CI 77266)'、'氧化铁 (CI 77491、77492、77499)'、'群青 (CI 77007)']

Say I have a string like this, where items are separated by commas but there may also be commas within items that have parenthesized content:

(EDIT: Sorry, forgot to mention that some items may not have parenthesized content)

"Water, Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"

How can I split the string by only those commas that are NOT within parentheses? i.e:

["Water", "Titanium Dioxide (CI 77897)", "Black 2 (CI 77266)", "Iron Oxides (CI 77491, 77492, 77499)", "Ultramarines (CI 77007)"]

I think I'd have to use a regex, perhaps something like this:

([(]?)(.*?)([)]?)(,|$)

but I'm still trying to make it work.

解决方案

Use a negative lookahead to match all the commas which are not inside the parenthesis. Splitting the input string according to the matched commas will give you the desired output.

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

DEMO

>>> import re
>>> s = "Water, Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"
>>> re.split(r',\s*(?![^()]*\))', s)
['Water', 'Titanium Dioxide (CI 77897)', 'Black 2 (CI 77266)', 'Iron Oxides (CI 77491, 77492, 77499)', 'Ultramarines (CI 77007)']

这篇关于如何用不在括号内的逗号分隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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