python - 如何在字符串中搜索任何数字后跟特定单词?

标签 python regex

我有一个段落已分成几行并删除了所有标点符号。现在我想检查是否有任何行包含任何数字,后跟“度”一词,以便我可以输出它。

例如,在句子上

房间温度为32度

我想找到子字符串

32度

关于这句话

该刚体有 6 个自由度

我想找到子字符串

6 度

是否有办法一致地找到前面有任何数字的特定单词?

最佳答案

这是我的看法:

Python 3.7.4 (default, Aug 12 2019, 14:45:07) 
[GCC 9.1.1 20190605 (Red Hat 9.1.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> degree=re.compile(r'\d+\s*degree[s]?')
>>> s='32 degrees should be matched as 0 degree and 0degree should be as well, but not this last "degree" here.'
>>> degree.findall(s)
['32 degrees', '0 degree', '0degree']
>>>

关于python - 如何在字符串中搜索任何数字后跟特定单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58632769/

相关文章:

python - 如果值为 None,如何提供默认值?

python - 如果缺少一件​​元素,则 Scrapy 不会抓取

javascript - 正则表达式查找标签和变量

java - 如何在 java 中解析单词创建的特殊字符

正则表达式 - 匹配 URL,除了个别情况

python - urllib.error.HTTPError : HTTP Error 503: Service Unavailable python

Python连续读取文件

python - Python 正则表达式中的错误? (re.sub with re.MULTILINE)

regex - 如果扩展名是大写,primefaces 文件上传预览将不起作用

python - 如何对 "signal"感兴趣的子进程(无信号)?