我有以下文本
txt = 'Lithium 0.25 (7/11/77). LFTS wnl. Urine tox neg. Serum tox + fluoxetine 500; otherwise neg. TSH 3.28. BUN/Cr: 16/0.83. Lipids unremarkable. B12 363, Folate >20. CBC: 4.9/36/308 Pertinent Medical Review of Systems Constitutional:'我想在上面的表达式中获得日期,我写了下面的表达式。
re.findall(r'(?:[\d{1,2}]+)(?:[/-]\d{0,}[/-]\d{2,4})', txt)如果我执行上述表达式,则会显示以下输出
'7/11/77','9/36/308‘
我不希望"4.9/36/308“这是包括在内,我必须为此更改正则表达式。
请帮帮忙。
发布于 2017-10-03 17:57:36
您可以将当前的正则表达式修复为
\b(?<!\.)\d{1,2}[/-]\d+[/-]\d{2,4}\b请参阅regex demo
\b将匹配单词边界,如果在匹配第一个数字之前存在.,则(?<!\.)负向回溯将使匹配失败。
请参阅Python demo。
请注意,如果只需要获取有效日期列表,则必须在以后使用non-regex method。
https://stackoverflow.com/questions/46541816
复制相似问题