使用
import re
x = "John Doe, Aug 5 2020 Hello Jane Doe: Aug 5 2020"
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
rx = re.compile(fr".*?\b(?:{'|'.join(months)})\s+\d+\s+\d+", re.I)
print([m.strip() for m in rx.findall(x)])
结果:['John Doe, Aug 5 2020', 'Hello Jane Doe: Aug 5 2020']
见Python proof。
正则表达式:
.*?\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d+\s+\d+
解释
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
Jan 'Jan'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Feb 'Feb'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Mar 'Mar'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Apr 'Apr'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
May 'May'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Jun 'Jun'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Jul 'Jul'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Aug 'Aug'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Sep 'Sep'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Oct 'Oct'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Nov 'Nov'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Dec 'Dec'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))