簡體   English   中英

正則表達式在某些指定的單詞之后提取日期

[英]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})
  • \\d : 代表所有數字,即 [0-9]
  • {1,2} : 長度,即最少 1 個或最多 2 個字符
  • [AZ] : 允許 AZ 所有字母

我希望這個對你有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM