2

I want to get the date followed by DATE FILLED and REFILL from my text. Ordering of DATE FILLED or REFILL is not fixed. And date pattern can be:

6/23/20
6-23-20

My Python scripts is:

expiration_date_regex = re.compile(r"(USE\s+BY.*(?P<expiration>\d{1,2}/\d{1,2}/\d{2,4}))|(DATE\s+FILLED.*(?P<date_filled>\d{1,2}/\d{1,2}/\d{2,4}))", re.M)
find_matches(expiration_date_regex, text)

def find_matches(regex, text):
    matches = regex.findall(text)
    for match in matches:
        print(match)

My text is:

CVS pharmacy
713-217 HsonSt
OTY: 90
REFILL 0 Refills
PRSCBN. A Beil
DATE FILLED 6/23/20
USE BY. 6/23/21
RPH Bill Liu
MFR AUROBINDO PHARM
ST DEA BC2236645
This is a WHITE
REDTME

But I'm getting output something like this, output is almost reasonable but I don't understand what do those first two empty strings means in the first tuple? same goes for last two string of the second tuple. It looks something like bitmask:

('', '', 'DATE FILLED 6/23/20', '6/23/20')
('USE BY. 6/23/21', '6/23/21', '', '')
4
  • 3
    Why not use simpler: (USE\s+BY|DATE\s+FILLED).*(?P<date>\d{1,2}/\d{1,2}/\d{2,4}) Commented Jan 2, 2021 at 6:01
  • 1
    Thanks @anubhava your comment gives a solution. I had few question how can I handle if Use by and date fill is different date pattern? Is it better to use a single regex with group or write different for each? My use case is I'm getting those texts from an OCR and then finding my required patterns. Sometimes I may get Dat Fill instead of Date Filled Commented Jan 2, 2021 at 6:17
  • Or may be: (?i)(USE\s+BY|DATE\s+FILL(?:ED)?).*(?P<date>\d{1,2}[-/]\d{1,2}[-/]\d{2,4}) Commented Jan 2, 2021 at 6:21
  • 2
    Thanks again @anubhava it worked. Hope to read your book soon. Commented Jan 2, 2021 at 6:29

1 Answer 1

3

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this regex:

(?i)(USE\s+BY|DATE\s+FILL(?:ED)?).*(?P<date>\d{1,2}[-/]\d{1,2}[-/]\d{2,4})

RegEx Demo

RegEx Details:

  • (?i): Enable ignore case mode
  • (USE\s+BY|DATE\s+FILL(?:ED)?): Match USE BY or DATE FILLED or DATE FILL
  • .*: Match 0 or more of any characters
  • (?P<date>\d{1,2}[-/]\d{1,2}[-/]\d{2,4}): Match a date string using / or - as delimiter
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.