我只需要使用python从字符串中提取2位数字即可。 我尝试了以下操作:
1. below will extract all the numbers in the sentence.
age =[int(s) for s in Text .split() if s.isdigit()]
2. Below code will extract only numbers.
age = re.findall(r'\d+', Text )
Text = I am sorry I am able 28 years old I have cough for 3 weeks online company with severe headache the headache is at its cost in the morning and have some people
Actual output : 28,3
Expected output : 28
答案 0 :(得分:2)
使用正则表达式边界\b
。
例如:
import re
Text = "I am sorry I am able 28 years old I have cough for 3 weeks online company with severe headache the headache is at its cost in the morning and have some people"
print(re.findall(r"\b\d{2}\b", Text))
输出:
['28']
答案 1 :(得分:1)
请尝试执行age = re.findall(r'\d+', Text )
,而不要执行步骤2的age = re.findall(r'\d\d', Text )
。