使用正则表达式和或为了摆脱不需要的字符
Using Regex and or in order to get rid of unwanted characters
我对正则表达式感到很困惑,我需要帮助。
我有以下字符串:
x='def{{{12.197835/// -0.001172, 12.19788 7.3E-5, //+{{12.196705 -1.7E-5, 12.196647 -0.001189///}}}Def'
此字符串是 pandasdataframe 中特定列中单元格的一部分。每个单元格都有不同的不需要的字符,主要是字母和“/”或“{”。
我想要这样的输出:
x='12.197835,-0.001172, 12.19788,7.3E-5,12.196705 ,-1.7E-5, 12.196647 -0.001189'
(去掉任何不是数字的东西,如果是前面有“-”的数字或者E-就是前面有数字的“E-”。
我使用了这个表达式来生成数字:
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']
但我的问题是这个表达式不保留“-”或 'E'。
我试图通过以下表达式保存它们:
print(re.findall(r"\d+\.*\d*",x) or (r"^-?[0-9]\d+\.*\d+*\[E-]",x))
但我得到相同的输出:
>>>['12.197835', '0.001172', '12.19788', '7.3', '5', '12.196705', '1.7', '5', '12.196647', '0.001189']
我想也许是因为我正在使用 or 然后它已经满足第一个条件所以我也尝试了“and”但是这给出了非常奇怪的结果:
>>>('^-?[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')
我的最终目标是获得第一个只有数字“-”和后面有“-”的 E(所需输出)
x='12.197835,-0.001172, 12.19788,7.3E-5,12.196705 ,-1.7E-5, 12.196647 -0.001189'
希望这对你有帮助(不使用正则表达式)。
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)
[结果]:
12.197835,-0.001172,12.19788,7.3E-5,+12.196705,-1.7E-5,12.196647,-0.001189
您可以使用
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
参见Python demo and the regex demo。
正则表达式详细信息
[+-]?
- 可选的 +
或 -
\d*
- 零个或多个数字
\.?
- 一个可选的 .
\d+
- 一位或多位数字
(?:[eE][+-]?\d+)?
- e
或 E
的可选出现,后跟可选的 +
或 -
,然后是一个或多个数字。
我对正则表达式感到很困惑,我需要帮助。 我有以下字符串:
x='def{{{12.197835/// -0.001172, 12.19788 7.3E-5, //+{{12.196705 -1.7E-5, 12.196647 -0.001189///}}}Def'
此字符串是 pandasdataframe 中特定列中单元格的一部分。每个单元格都有不同的不需要的字符,主要是字母和“/”或“{”。
我想要这样的输出:
x='12.197835,-0.001172, 12.19788,7.3E-5,12.196705 ,-1.7E-5, 12.196647 -0.001189'
(去掉任何不是数字的东西,如果是前面有“-”的数字或者E-就是前面有数字的“E-”。
我使用了这个表达式来生成数字:
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']
但我的问题是这个表达式不保留“-”或 'E'。 我试图通过以下表达式保存它们:
print(re.findall(r"\d+\.*\d*",x) or (r"^-?[0-9]\d+\.*\d+*\[E-]",x))
但我得到相同的输出:
>>>['12.197835', '0.001172', '12.19788', '7.3', '5', '12.196705', '1.7', '5', '12.196647', '0.001189']
我想也许是因为我正在使用 or 然后它已经满足第一个条件所以我也尝试了“and”但是这给出了非常奇怪的结果:
>>>('^-?[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')
我的最终目标是获得第一个只有数字“-”和后面有“-”的 E(所需输出)
x='12.197835,-0.001172, 12.19788,7.3E-5,12.196705 ,-1.7E-5, 12.196647 -0.001189'
希望这对你有帮助(不使用正则表达式)。
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)
[结果]:
12.197835,-0.001172,12.19788,7.3E-5,+12.196705,-1.7E-5,12.196647,-0.001189
您可以使用
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
参见Python demo and the regex demo。
正则表达式详细信息
[+-]?
- 可选的+
或-
\d*
- 零个或多个数字\.?
- 一个可选的.
\d+
- 一位或多位数字(?:[eE][+-]?\d+)?
-e
或E
的可选出现,后跟可选的+
或-
,然后是一个或多个数字。