I have a piece of text like this:
[{1,2,3,4}, 3, 5,2,4, {1,2}, {1,2,3,4}, {1,33,3443}, 1..10]
here the numbers within curly braces {}
are to be considered a single atom. Finally I want to split the text to individual elements in array. So finally the piece of text after some operation need to become a list like this
expected = ['{1,2,3,4}', '3', '5', '2', '4', '{1,2}', '{1,2,3,4}', '{1,33,3443}', '1..10']
with each element as separate strings.
I am not able to figure out a good way to split. I can take the string as an array and iterate through it and substitute ,
only within {}
with some other delimiter and use a split
function to split on ,
to get what I want. But I was wondering if it is possible via regular expression by applying substitution on portion of text that matches some pattern. I was trying to do it by doing something like this.
line='[{1,2,3,4}, 3, 5,2,4, {1,2}, {1,2,3,4}, {1,33,3443}]'
# I hope the comma in {1,2,3,4} are substituted by : and i get {1:2:3:4}
# on which i can do a re.split or just split to get elements in form i want
# find text within {} on the text found, replace ',' with ':'
re.sub(r'(?P<set_value>\{.*?\})', re.sub(r',',':', '\g<1>'), line)
when i run the above code, i get the original line itself, without any change
'[{1,2,3,4}, 3, 5,2,4, {1,2}, {1,2,3,4}, {1,33,3443}]'
Is there a way I can fix the expression get right answer?