Reputation: 37
I have a *string of letters and (negative)numbers and want to separate them into a array with python.
pathD = "M30,50.1c0,0,25,100,42,75s10.3-63.2,36.1-44.5s33.5,48.9,33.5,48.9l24.5-26.3"
splitS = re.split('(\d+)',pathD)
this does not work because it splits the dots and the dashes in one lined mess :
['M', '30', ',', '50', '.', '1', 'c', '0', ',', '0', ',', '25', ',', '100', ',', '42', ',', '75', 's', '10', '.', '3', '-', '63', '.', '2', ',', '36', '.', '1', '-', '44', '.', '5', 's', '33', '.', '5', ',', '48', '.', '9', ',', '33', '.', '5', ',', '48', '.', '9', 'l', '24', '.', '5', '-', '26', '.', '3', '']
i would like to see something like this:
[M, 30, 50.1, c, 0, 0, 25, 100, 42, 75, s, 10.3, -63.2, 36.1, -44.5, s, 33.5, 48.9, 33.5, 48.9, l, 24.5, -26.3]
I am not sure if i am on the right path with this one or should approach it differently.
Upvotes: 2
Views: 2960
Reputation: 5658
import re
pathD = "M30,50.1c0,0,25,100,42,75s10.3-63.2,36.1-44.5s33.5,48.9,33.5,48.9l24.5-26.3"
print(re.findall(r'[A-Za-z]|-?\d+\.\d+|\d+',pathD))
['M', '30', '50.1', 'c', '0', '0', '25', '100', '42', '75', 's', '10.3', '-63.2', '36.1', '-44.5', 's', '33.5', '48.9', '33.5', '48.9', 'l', '24.5', '-26.3']
Upvotes: 4