简体   繁体   中英

regular expression from regexr.com doesn't work in python

I have such a function:

def get_temperature(s):
        parts = re.findall(r'([+-]?\d+(\.\d+)*)\s?°([CcFf])', s)
        for a in range(len(parts)):
                s = s.replace(parts[a], "qTEMPq")
        return s

The input parameter s for the function is a string value. The output parameter is also a string value.

So at the end, if I have a string like "It is +25°C outside." as an input, the output string will be "It is qTEMPq outside."

The regular expression I am using from extracting temperature degrees (celcius or fahrenheit) from string finds the subparts similar to (40°F, +30°C, -35 °C, etc.). It works perfectly in regexr.com , but not in my code.

What might be the problem, and how can I solve it?

If you have more than 1 group (...) in your regex, findall will return a list of tuples.

If you want to obtain a list of strings, you can make the groups non-capturing using (?:...), as in:

import re
def get_temperature(s):
        parts = re.findall(r'(?:[+-]?\d+(?:\.\d+)*)\s?°(?:[CcFf])', s)
        for a in range(len(parts)):
                s = s.replace(parts[a], "qTEMPq")
        return s
get_temperature('40.5°F')
# 'qTEMPq'
get_temperature('100°F is nearly 37°C')
# 'qTEMPq is nearly qTEMPq'
get_temperature("It is +25°C outside.")
# 'It is qTEMPq outside.'

If what you want is to access the parts of the temperature, you could do (in order to have tuples with value and unit):

def get_temperature(s):
        parts = re.findall(r'([+-]?\d+(?:\.\d+)*)\s?°([CcFf])', s)
        return parts

get_temperature("It is +25°C outside.")
#[('+25', 'C')]

Or, if you just want to have the whole temperature as a string:

def get_temperature(s):
        parts = re.findall(r'(?:[+-]?\d+(?:\.\d+)*)\s?°(?:[CcFf])', s)
        return parts
get_temperature('100°F is nearly 37°C')
# ['100°F', '37°C']
import re
def get_temperature(s):
    return re.sub(r'[+-]?\d+\.*\d*\s?°[CcFf]', 'qTEMPq', s)

Is this what you're looking for?

I have solved the problem, by using " \\xb0 " instead of " ° ". It was an encoding issue. So basically, instead of using '[+-]?\\d+\\.*\\d*\\s?°[CcFf]' expression, I have used '[+-]?\\d+\\.*\\d*\\s?\\xb0[CcFf]' .

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