简体   繁体   English

分割逗号分隔的字符串

[英]Splitting a comma-separated string

I have following string, and what I would like to have is split it to get an array of key:value pairs 我有以下字符串,我想将其拆分成键:值对数组

color:'White', color:('White' or 'Black'),color:'YELLOW,BLACK', price: [11,12], price:{13, 14}, price:[11,13 },price:{ 11,13], color:('White' and 'Black')

Given above string, I would like to get an array with following elements - 给定上面的字符串,我想得到一个包含以下元素的数组-

color:'White'
color:('White' or 'Black')
color:'YELLOW,BLACK'
price:[11,12]
price:{13, 14}
price:[11,13 }
price:{ 11,13]
color:('White' and 'Black')

I can do the above by parsing the string character by character and appropriately forming groups. 我可以通过按字符解析字符串并适当地形成组来完成上述操作。 I tried some regexp, however, all of them seemed to fail. 我尝试了一些正则表达式,但是它们似乎都失败了。

I am trying to achieve this in Python, not sure if that would matter. 我正在尝试在Python中实现这一点,不确定是否会很重要。 Here's what I tried - 这是我尝试过的-

re.split(r'(, *(?=.*:))', "color:'White',color:('White' or 'Black'),color:'DEF,GHI',price:[11,12], price:{13,14}, price:[11,13},price:{11,13]")

The characters between single quotes can be anything, mixture of alpha-numeric, unicode, etc as text may pertain to different languages. 单引号之间的字符可以是任何字符,也可以是字母数字,unicode等的混合字符,因为文本可能涉及不同的语言。

This should do it: 应该这样做:

re.split(', *(?=[^,]+?:)',string)

Meaning split on commas only if followed by zero-or-more spaces and a sequence of characters (excluding commas and colons) terminated with a colon. 仅当逗号后接零个或多个空格以及以冒号结尾的一系列字符(不包括逗号和冒号)时,才对逗号进行分割。

With your string, it gives: 使用您的字符串,它给出:

["color:'White'",
 "color:('White' or 'Black')",
 "color:'YELLOW,BLACK'",
 'price: [11,12]',
 'price:{13, 14}',
 'price:[11,13 }',
 'price:{ 11,13]']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM