gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:11:10 25 4
gpt4 key购买 nike

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

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

enter image description here

我需要在正则表达式中添加什么,这样我才能正确地得到它们之间有空格的那些?比如50 space ° space C应该看成一个整体,而不是三 block 。

最佳答案

尝试使用这种模式:

\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']

关于python - 用于匹配温度的正则表达式 (°c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56042140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com