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.

codeBarer
  • 2,238
  • 7
  • 44
  • 75
  • 3
    The regex escaping symbol is ``\``, not `/`. To extract all matches [use `re.findall`](https://stackoverflow.com/questions/4697882/how-can-i-find-all-matches-to-a-regular-expression-in-python). – Wiktor Stribiżew Nov 29 '17 at 15:00
  • 1
    Backslashes instead of forward slashes: `\d` not `/d` – ctwheels Nov 29 '17 at 15:00
  • I tried the \d{2}-\d{2}-\d{4} but I'm string note getting any values – codeBarer Nov 29 '17 at 15:05
  • 2
    @codeBarer put an `r` before the string so that it's `r"\d{2}-\d{2}-\d{4}"` – ctwheels Nov 29 '17 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 – ctwheels Nov 29 '17 at 15:14

2 Answers2

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']]
rahlf23
  • 8,869
  • 4
  • 24
  • 54
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
ZiTAL
  • 3,466
  • 8
  • 35
  • 50