Using Python and Regex to extract different formats of dates

Kartheek Palepu

I have the following code to match the dates

import re
date_reg_exp2 = re.compile(r'\d{2}([-/.])(\d{2}|[a-zA-Z]{3})\1(\d{4}|\d{2})|\w{3}\s\d{2}[,.]\s\d{4}')
matches_list = date_reg_exp2.findall("23-SEP-2015 and 23-09-2015 and 23-09-15 and Sep 23, 2015")
print matches_list

The output I expect is

["23-SEP-2015","23-09-2015","23-09-15","Sep 23, 2015"]

What I am getting is:

[('-', 'SEP', '2015'), ('-', '09', '2015'), ('-', '09', '15'), ('', '', '')]

Please check the link for regex here.

Wiktor Stribiżew

The problem you have is that re.findall returns captured texts only excluding Group 0 (the whole match). Since you need the whole match (Group 0), you just need to use re.finditer and grab the group() value:

matches_list = [x.group() for x in date_reg_exp2.finditer("23-SEP-2015 and 23-09-2015 and 23-09-15 and Sep 23, 2015")]

See IDEONE demo

re.findall(pattern, string, flags=0)
Return all non-overlapping matches of pattern in string, as a list of strings... If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

re.finditer(pattern, string, flags=0)
Return an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Extract phone number using regex with different formats python

From Dev

Regex for different prices formats

From Dev

Comparing dates in Xpath with different formats

From Dev

Match dates with different formats in Powershell

From Dev

javascript - compare dates in different formats

From Dev

Match dates with different formats in Powershell

From Dev

Python regex to match 6-digit numbers of different formats

From Dev

Using Regex to extract Data to different Columns in Pandas

From Dev

Using Regex to extract Data to different Columns in Pandas

From Dev

Multiple Regex Pattern with different formats

From Dev

Regex for capturing different date formats

From Dev

Regex to capture timestamp in different formats

From Dev

Subtracting Dates of Different Formats and converting to Int

From Dev

Parsing two different formats of dates in data frame

From Dev

Subtracting Dates of Different Formats and converting to Int

From Dev

Error while comparing dates in different formats

From Dev

Parsing two different formats of dates in data frame

From Dev

SAS Proc Compare - dates in different formats

From Dev

Java or Scala fast way to parse dates with many different formats using java.time

From Dev

How to extract the date from a DateField into different formats?

From Dev

python compound regex to extract text between different tags in different documents

From Dev

Regex expression to extract dates in text?

From Dev

Extract dates from url with regex

From Dev

Parsing dates in multiple formats in R using lubridate

From Dev

Python - Extract pattern from string using RegEx

From Dev

using python regex to extract clean URLs

From Dev

extract specific text using multiple regex in python?

From Dev

Extract word from string Using python regex

From Dev

Extract string and assign to the variable using regex in Python

Related Related

  1. 1

    Extract phone number using regex with different formats python

  2. 2

    Regex for different prices formats

  3. 3

    Comparing dates in Xpath with different formats

  4. 4

    Match dates with different formats in Powershell

  5. 5

    javascript - compare dates in different formats

  6. 6

    Match dates with different formats in Powershell

  7. 7

    Python regex to match 6-digit numbers of different formats

  8. 8

    Using Regex to extract Data to different Columns in Pandas

  9. 9

    Using Regex to extract Data to different Columns in Pandas

  10. 10

    Multiple Regex Pattern with different formats

  11. 11

    Regex for capturing different date formats

  12. 12

    Regex to capture timestamp in different formats

  13. 13

    Subtracting Dates of Different Formats and converting to Int

  14. 14

    Parsing two different formats of dates in data frame

  15. 15

    Subtracting Dates of Different Formats and converting to Int

  16. 16

    Error while comparing dates in different formats

  17. 17

    Parsing two different formats of dates in data frame

  18. 18

    SAS Proc Compare - dates in different formats

  19. 19

    Java or Scala fast way to parse dates with many different formats using java.time

  20. 20

    How to extract the date from a DateField into different formats?

  21. 21

    python compound regex to extract text between different tags in different documents

  22. 22

    Regex expression to extract dates in text?

  23. 23

    Extract dates from url with regex

  24. 24

    Parsing dates in multiple formats in R using lubridate

  25. 25

    Python - Extract pattern from string using RegEx

  26. 26

    using python regex to extract clean URLs

  27. 27

    extract specific text using multiple regex in python?

  28. 28

    Extract word from string Using python regex

  29. 29

    Extract string and assign to the variable using regex in Python

HotTag

Archive