Regex for Month-Day, Delimiter on Wrong Side

Viewed 32

I have this code:

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"\s+(?=(?:{'|'.join(months)})\b)", re.I)
print(rx.split(x))

Which outputs this:

['John Doe,', 'Aug 5 2020 Hello Jane Doe:', 'Aug 5 2020']

I would like it to output this:

["John Doe, Aug 5 2020", "Hello Jane Doe: Aug 5 2020"]

How could I do this? Thank you in advance for all the help!

2 Answers

Instead of split you can use findall using this approach:

>>> rx = re.compile(fr"\b\S.*?(?:{'|'.join(months)})" + r"\s+\d{1,2}\s+\d{4}", re.I)
>>> print(rx.findall(x))
['John Doe, Aug 5 2020', 'Hello Jane Doe: Aug 5 2020']

In this regex, we start the match from a word boundary and a non-whitespace character and match anything until we find this date string which is an alternation of months followed by date and year part.

RegEx Demo

Use

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)])

Results: ['John Doe, Aug 5 2020', 'Hello Jane Doe: Aug 5 2020']

See Python proof.

Regex:

.*?\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d+\s+\d+

EXPLANATION

--------------------------------------------------------------------------------
  .*?                      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))
Related