We have answer of your question!

100% solved queries, no empty question

Question: Capture the date from emails using RegEx


0

Here are 3 snippets from 3 different emails:

1) Subject: FW: NEFS 11 fish for lease
   From: Claire Fitz-Gerald 
   Date: 11/15/2013 3:02 PM

2) Subject: FW: NEFS 11 and 12 fish for lease
   From: Claire Fitz-Gerald 
   Date: 11/11/2013 4:09 PM

3) Subject: FW: NEFS 11 fish for lease
   From: Claire Fitz-Gerald 
   Date: 12/5/2013 4:23 PM

I am trying to capture the date from these emails, and 100's more, but can't seem to utilize RegEx correctly. For one, I am not an expert with RegEx. But I have seen similar posts on StackOverflow and tried using their code but for some reason it doesn't work for me.

My code:

with open(file_path, 'r') as f:
pattern = re.compile("(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d")
        email = f.read()
        dates = pattern.findall(email)
        if dates:
            #print("Date:", ''.join(dates))
            print("Date:", ''.join(''.join(dates) for dates in dates))

I am confused why this RegEx seems to work for others but not me. I have also tried using much more in depth RegEx I found on SO:

re.compile("^((0?[13578]|10|12)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[01]?))(-|\/)((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1}))|(0?[2469]|11)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[0]?))(-|\/)((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1})))$")

I would simply like to capture the date in these emails, and then I can worry about turning them into the correct format later. Any help is appreciated, thanks.

Question author Theprowler | Source

Answer


1


To capture the dates you can use this code:

regex = r"Date: (\d{1,2}\/\d{1,2}\/\d{4})"

Check online demo. You can also take a look at Useful Regex Showcase on the SO doc.

Answer author Thomas-ayoub

Tickanswer.com is providing the only single recommended solution of the question Capture the date from emails using RegEx under the categories i.e python , regex , email , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!