使用 python 从句子中仅提取 2 位数字的方法是什么

what is the way to extract only 2 digits numbers from the sentence using python

我只需要使用 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

使用正则表达式边界 \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']

不要在第 2 步中执行 age = re.findall(r'\d+', Text ),而是尝试执行 age = re.findall(r'\d\d', Text )