简体   繁体   中英

Using Regex and or in order to get rid of unwanted characters

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'

You may use

import re
x='def{{{12.197835/// -0.001172, 12.19788 7.3E-5, //+{{12.196705 -1.7E-5, 12.196647 -0.001189///}}}Def'
print(re.findall(r'[+-]?\d*\.?\d+(?:[eE][+-]?\d+)?', x))  # Extracting all numbers into a list
# => ['12.197835', '-0.001172', '12.19788', '7.3E-5', '12.196705', '-1.7E-5', '12.196647', '-0.001189']
print(",".join(re.findall(r'[+-]?\d*\.?\d+(?:[eE][+-]?\d+)?', x))) # Creating a comma-separated string
# => 12.197835,-0.001172,12.19788,7.3E-5,12.196705,-1.7E-5,12.196647,-0.001189

See the Python demo and the regex demo .

Regex details

  • [+-]? - an optional + or -
  • \d* - zero or more digits
  • \.? - an optional .
  • \d+ - one or more digits
  • (?:[eE][+-]?\d+)? - an optional occurrence of e or E followed with an optional + or - and then one or more digits.

Hope this help you (without using regex).

x='def{{{12.197835/// -0.001172, 12.19788 7.3E-5, //+{{12.196705 -1.7E-5, 12.196647 -0.001189///}}}Def'

x=x.replace('{','').replace('}','').replace('def','').replace('Def','').replace('/','').replace('  ',' ').replace(' ',',').replace(',,',',')

print(x)

[Result]:

12.197835,-0.001172,12.19788,7.3E-5,+12.196705,-1.7E-5,12.196647,-0.001189

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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