![](/img/trans.png)
[英]Python extract 3 words before and 3 words after a specific list of words with a regex
[英]Regex to extract dates after some specified words
我想從這些文本中提取出生日期。 有四個條件:
keywords to match
Date of Birth
date of birth:
D.O.B:
DOB:
像這樣的字符串:
text = "my name is adam my DOB: 30-11-1988 and i play football since 20-2011"
text = "my name is D.O.B: 20/MAY/1987 and i play football since 20-2011"
text = "my Date of Birth 12-7-1970 and i start study since 20-2011"
text = "my date of birth: 20/9/1970 "
你可以這樣做:
regex = r'(Date of Birth|date of birth|D.O.B|DOB):\s(\S*)'
您的第 2 組將包含任何格式的捕獲日期。
現場演示在這里
解釋
(Date of Birth|date of birth|DOB|DOB)
:尋找dob 短語的任何替代詞。:\\s
:查找冒號后跟空格字符。(\\S*)
:尋找任何沒有。 連續的非空格字符,這是你想要的日期。(DOB: \d{1,2}-\d{1,2}-\d{1,4}|D.O.B: \d{1,2}\/[A-Z]{1,}\/\d{1,4}|Date of Birth \d{1,2}-\d{1,2}-\d{1,4}|date of birth: \d{1,2}\/\d{1,2}\/\d{1,4})
我希望這個對你有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.