Reputation: 5516
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. 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.
Upvotes: 0
Views: 301
Reputation: 19601
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]']
Upvotes: 1