Extracting Temperature Degrees (celcius Or Fahrenheit) From String
I am using (char.*?char2) in order to extract subparts which starts with char1 and which ends with char2 from a string. Now I want to extract temperature information e.g. (40 °C,
Solution 1:
(\d+) ?°([CF])
The first group should have the temperature, the second C or F.
Expanding it to allow for a bit more variation:
([+-]?\d+(\.\d+)*)\s?°([CcFf])
This would match any of these inputs, allowing for more than one space, or tab, lower case unit, decimal points and signs.
Example python program:
import re
string ='''
20°C
2 °F
It was cold, 2 °F in fact.
30 °C
-40 °C
+2.3^I°c
+2.3°c
10°C
'''pattern= r'([+-]?\d+(\.\d+)*)\s?°([CcFf])'
print(re.findall(pattern, string))
# Output:
# [('20', '', 'C'), ('2', '', 'F'), ('2', '', 'F'), ('30', '', 'C'),
# ('-40', '', 'C'), ('+2.3', '.3', 'c'), ('+2.3', '.3', 'c'),
# ('10', '', 'C')]
Post a Comment for "Extracting Temperature Degrees (celcius Or Fahrenheit) From String"