用于匹配温度的正则表达式 (°c)

RegEx for matching temperatures (°c)

我想获得所有 temperature/temperature 范围,它们之间有和没有 space。现在,我可以使用以下方法获得它们之间没有 spaces 的那些:

re.findall(r'[0-9°c-]+', text)

我需要在正则表达式中添加什么,以便我也可以正确地得到它们之间带有 spaces 的那些?比如50space°spaceC应该看成一个整体,而不是三块

尝试使用此模式:

\d+°c(?:\s*-\d+°c)?

示例脚本:

input = "It is 50°c today.  One range is 30°c-40°c and here is another 10°c -20°c"
matches = re.findall(r'\d+°c(?:\s*-\d+°c)?', input)
print(matches)

['50\xc2\xb0c', '30\xc2\xb0c-40\xc2\xb0c', '10\xc2\xb0c -20\xc2\xb0c']

您可以使用

-?\d+(?:\.\d+)?\s*°\s*c(?:\s*-\s*-?\d+(?:\.\d+)?\s*°\s*c)?

regex demo

该模式由一个 -?\d+(?:\.\d+)?\s*°\s*c 块组成,该块重复两次(以匹配可选范围部分)并匹配负温度值和小数温度值:

  • -? - 一个可选的连字符
  • \d+ - 1+ 位
  • (?:\.\d+)? - 可选小数部分
  • \s* - 0+ 个空格
  • ° - 度数符号
  • \s* - 0+ 个空格
  • c - c 字符

(?:\s*-\s*<ABOVE_BLOCK>)? 匹配 1 次或 0 次重复的用 0+ 个空格括起来的连字符,然后匹配上述相同的块。

在Python中,动态构建模式是有意义的:

tb = r'-?\d+(?:\.\d+)?\s*°\s*c'
rx = r'{0}(?:\s*-\s*{0})?'.format(tb)
results = re.findall(rx, s)

如果 c 是可选的,请将 \s*c 替换为 (?:\s*c)?

如果 °c 是可选的,请将 \s*°\s*c 替换为 (?:\s*°\s*c)?(?:\s*°(?:\s*c)?)?

这是温度块模式,其中度数符号和 c 字符都是可选的,但遵循与之前相同的顺序:

tb = r'-?\d+(?:\.\d+)?(?:\s*°(?:\s*c)?)?'

完整 Python demo code:

import re
s = 'This is some temperature 30° c - 50 ° c  2°c  34.5 °c 30°c - 40 °c and "30° - 40, and -45.5° - -56.5° range' 
tb = r'-?\d+(?:\.\d+)?(?:\s*°(?:\s*c)?)?'
rx = r'{0}(?:\s*-\s*{0})?'.format(tb)
results = re.findall(rx, s)
print(results)
# => ['30° c - 50 ° c', '2°c', '34.5 °c', '30°c - 40 °c', '30° - 40', '-45.5° - -56.5°']

如果度数符号可能丢失而 c 可能仍然存在,请移动分组边界:

tb = r'-?\d+(?:\.\d+)?(?:\s*°)?(?:\s*c)?'
                      ^-------^^-------^

this regex demo and the full Python code demo:

import re
s = 'This is some temperature 30° c - 50 ° c  2°c  34.5 °c 30°c - 40 °c and "30° - 40, and -45.5° - -56.5° range 30c - 50 °c" or 30c - 40' 
tb = r'-?\d+(?:\.\d+)?(?:\s*°)?(?:\s*c)?'
rx = r'{0}(?:\s*-\s*{0})?'.format(tb)
results = re.findall(rx, s)
print(results)

输出:

['30° c - 50 ° c', '2°c', '34.5 °c', '30°c - 40 °c', '30° - 40', '-45.5° - -56.5°', '30c - 50 °c', '30c - 40']

This expression 可能会帮助您这样做:

(([0-9°c\s]+)(?:-[0-9°]+c))|([0-9°\s]+c)

图表

此图显示了表达式的工作原理,如果您想了解此 link 中的其他表达式,您可以将其形象化:

示例测试

const regex = /(([0-9°c\s]+)(?:-[0-9°]+c))|([0-9°\s]+c)/gm;
const str = `This is some temperature 30°c-40°c. 50 ° c. 30°c -40°c`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Python 测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(([0-9°c\s]+)(?:-[0-9°]+c))|([0-9°\s]+c)"

test_str = "This is some temperature 30°c-40°c. 50 ° c. 30°c -40°c"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

不带度数符号试试这个:

温度在 33.0 - 42.0 C 之间的正则表达式:

正则表达式为“(3[3-9]|4[0-1]{1}).|(4[2-2]{1}).[C|c]”

注意:您在 42.0 之前和 33.0 之后所有值都将带有 .1 到 .9 但在 42.0 之后它将抛出消息输入温度在 33.0-42.0 之间