0

I'm trying to extract dates out of a string using python. The date is in the format mm-dd-yyyy. So I know the regex should be something like /d{2}-/d{2}-/d{4}. However when I try and iterate over the array below I am not able to extract the dates out of the string.

import re 
logs = ["First entry to journal logs. (01-01-2015)", "Last entry to journal logs 07-01-2016"]
for i in logs:
    m = re.match("/d{2}-/d{2}-/d{4}",i)
    print m.group(0)

I haven't work with re before so not sure if I'm using it properly.

5
  • 3
    The regex escaping symbol is \, not /. To extract all matches use re.findall. Commented Nov 29, 2017 at 15:00
  • 1
    Backslashes instead of forward slashes: \d not /d Commented Nov 29, 2017 at 15:00
  • I tried the \d{2}-\d{2}-\d{4} but I'm string note getting any values Commented Nov 29, 2017 at 15:05
  • 2
    @codeBarer put an r before the string so that it's r"\d{2}-\d{2}-\d{4}" Commented Nov 29, 2017 at 15:06
  • 1
    @codeBarer you also want to use search instead of match. The latter is anchored at the start of the string such that your regex becomes ^\d{2}-\d{2}-\d{4}, while the former searches anywhere in the string Commented Nov 29, 2017 at 15:14

2 Answers 2

2

This should do the trick:

import re

s = ["First entry to journal logs. (01-01-2015)", "Last entry to journal logs 07-01-2016"]

print([re.findall(r'\d{2}\-\d{2}\-\d{4}', i) for i in s])

Yields:

[['01-01-2015'], ['07-01-2016']]
Sign up to request clarification or add additional context in comments.

Comments

1

You can't use match to search inside a text, If you use match you need to match from Initial position to Final position, to search inside a text use search:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re
logs = ["First entry to journal logs. (01-01-2015)", "Last entry to journal logs 07-01-2016"]
for i in logs:
    m = re.search("[0-9]{2}\-[0-9]{2}\-[0-9]{4}",i)
    if m:
        print m.group(0)

Output:

01-01-2015
07-01-2016

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.