I'm getting verry confused with the regex and I need help. I have the following string:
x='def{{{12.197835/// -0.001172, 12.19788 7.3E-5, //+{{12.196705 -1.7E-5, 12.196647 -0.001189///}}}Def'
This string is part of cell in specific column in pandasdataframe. each cell has different unwanted characters, mainly letters and "/" or "{".
I want to have this output:
x='12.197835,-0.001172, 12.19788,7.3E-5,12.196705 ,-1.7E-5, 12.196647 -0.001189'
(get rid of anything that is not a digit, beside if is a number with "-" before or E- which is "E-" with digit before.
I have used this expression in order to ger inly the digits:
print(re.findall(r"\d+\.*\d*",x))
>>>['12.197835', '0.001172', '12.19788', '7.3', '5', '12.196705', '1.7', '5', '12.196647', '0.001189']
but my problem is that this expression does not preserve the '-' or the 'E'. I have tried to save them by the following expression:
print(re.findall(r"\d+\.*\d*",x) or (r"^-?[0-9]\d+\.*\d+*\[E-]",x))
but I get the same output:
>>>['12.197835', '0.001172', '12.19788', '7.3', '5', '12.196705', '1.7', '5', '12.196647', '0.001189']
I thought maybe is because i'm using or and then it alreay satisfy the first condition so I tried also "and" but that gives very weird results:
>>>('^-?[0-9]\\d+\\.*\\d+*\\[E-]', 'def{{{12.197835/// -0.001172, 12.19788 7.3E-5, //+{{12.196705 -1.7E-5, 12.196647 -0.001189///}}}Def')
My end goal is to get the first string with only digits, '-' and E that has after it '-' (the desired output)
x='12.197835,-0.001172, 12.19788,7.3E-5,12.196705 ,-1.7E-5, 12.196647 -0.001189'