python - RegEx 匹配温度 (°c)
问题描述
我想获得所有温度/温度范围,它们之间有和没有空格。现在,我可以使用以下方法获取它们之间没有空格的那些:
re.findall(r'[0-9°c-]+', text)
我需要在正则表达式中添加什么,以便我可以正确地获得它们之间有空格的那些?例如50空间°空间C应该被看作一个整体而不是三个部分。
解决方案
尝试使用这种模式:
\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']
推荐阅读
- winforms - 在 Rider WinForms 设计器中选择控件时遇到问题
- python - Python 中的 Chrome 驱动程序错误 SessionNotCreatedException
- selenium-webdriver - 黄瓜定义的步骤,在运行时声明为未定义
- cygwin - 运行脚本 prepare_lang.sh 时出现 Kaldi 错误——GenericRegister::GetEntry: No such file or directory
- python - Django - 如何更改管理员面板中的个人资料视图?
- python - Kivy - 布局的位置产生问题
- html - 使用 Azure 托管私有项目的 docs/html 文件的位置
- javascript - 如何在 django 中获取更新 url - ajax
- html - 我无法正确指定图像的宽度和高度
- reactjs - 如何为“MM-DD-YYYY”(07-22-2021)等日期格式覆盖 React-Table 默认排序?