我需要 python 的正則運算式模式。或者,如果您有任何其他方法,請隨時分享。
我已經撰寫了用于提取僅提取數值的句子的正則運算式(模式:([^.]*?\d [^.]*\.))。但我不知道如何對該數值施加約束。
僅提取數值句子的模式 ([^.]*?\d [^.]*\.)
例子:
The patient is suffering from fever. Their relatives come to visit them. The patient age is 20 year. His brother could not visit him due to some other work. There is another patient whose age is 30 year old. The second patient is watching him from window.
輸出:
['The patient age is 20 year', 'There is another patient whose age is 30 year old']
uj5u.com熱心網友回復:
如果我們假設點只會出現在句子的結尾句點,那么以下方法可能有效:
inp = 'The patient is suffering from fever. Their relatives come to visit them. The patient age is 20 year. His brother could not visit him due to some other work. There is another patient whose age is 30 year old. The second patient is watching him from window.'
matches = re.findall(r'\s*([^.]*\b(?:[1-7]?[0-9]|8[0-5])\b[^.]*\.)', inp)
print(matches)
這列印:
['The patient age is 20 year.', 'There is another patient whose age is 30 year old.']
請注意,(?:[1-7]?[0-9]|8[0-5])正則運算式的部分匹配 0 到 85(包括 0 到 85)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/342411.html
