Как извлечь из предложения только 2-значные числа из предложения? - PullRequest
0 голосов
/ 13 декабря 2018

Мне нужно извлечь только 2-значные числа из строки, используя python.Я пробовал следующие вещи:

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

Ответы [ 2 ]

0 голосов
/ 13 декабря 2018

вместо age = re.findall(r'\d+', Text ) на шаге 2 попробуйте age = re.findall(r'\d\d', Text ).

0 голосов
/ 13 декабря 2018

Использовать границы регулярных выражений \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']
...