我有一个段落已分成几行并删除了所有标点符号。现在我想检查是否有任何行包含任何数字,后跟“度”一词,以便我可以输出它。
例如,在句子上
房间温度为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/