我有一个 .txt 文件,我必须从中获取姓名和年龄。.txt 文件的数据格式如下:
Age: 71 . John is 47 years old. Sam; Born: 05/04/1989(29).
Kenner is a patient Age: 36 yrs Height: 5 feet 1 inch; weight is 56 kgs.
This medical record is 10 years old.
Output 1: John, Sam, Kenner
Output_2: 47, 29, 36
我正在使用正则表达式来提取数据。例如,对于年龄,我使用以下正则表达式:
re.compile(r'age:\s*\d{1,3}',re.I)
re.compile(r'(age:|is|age|a|) \s*\d{1,3}(\s|y)',re.I)
re.compile(r'.* Age\s*:*\s*[0-9]+.*',re.I)
re.compile(r'.* [0-9]+ (?:year|years|yrs|yr) \s*',re.I)
我将对这些正则表达式的输出应用另一个正则表达式来提取数字。问题在于这些正则表达式,我也得到了我不想要的数据。例如
This medical record is 10 years old.
我从上面的句子中得到了“10”,这是我不想要的。我只想提取人名和年龄。我想知道应该是什么方法?我将不胜感激任何帮助。