Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Extract date in String

Python – Extract date in String

Given a string, the task is to write a Python program to extract date from it.

Input : test_str = "gfg at 2021-01-04"
Output : 2021-01-04
Explanation : Date format string found.

Input : test_str = "2021-01-04 for gfg"
Output : 2021-01-04
Explanation : Date format string found.

Method #1 : Using re.search() + strptime() methods

In this, the search group for a particular date is fed into search(), and strptime() is used to feed in the format to be searched.

Python3




# Python3 code to demonstrate working of
# Detect date in String
# Using re.search() + strptime()
import re
from datetime import datetime
 
# initializing string
test_str = "gfg at 2021-01-04"
 
# printing original string
print("The original string is : " + str(test_str))
 
# searching string
match_str = re.search(r'\d{4}-\d{2}-\d{2}', test_str)
 
# computed date
# feeding format
res = datetime.strptime(match_str.group(), '%Y-%m-%d').date()
 
# printing result
print("Computed date : " + str(res))


Output

The original string is : gfg at 2021-01-04
Computed date : 2021-01-04

Method #2: Using python-dateutil() module

This is another way to solve this problem. In this inbuilt Python library python-dateutil, The parse() method can be used to detect date and time in a string. 

Python3




# Python3 code to demonstrate working of
# Detect date in String
# Using python-dateutil()
from dateutil import parser
 
# initializing string
test_str = "gfg at 2021-01-04"
 
# printing original string
print("The original string is : " + str(test_str))
 
# extracting date using inbuilt func.
res = parser.parse(test_str, fuzzy=True)
 
# printing result
print("Computed date : " + str(res)[:10])


Output:

The original string is : gfg at 2021-01-04
Computed date : 2021-01-04

Method #3: Using string manipulation

Approach

We can use string manipulation to search for the date format string in the input string.

Algorithm

1. Split the input string into words.
2. Iterate through the words and check if each word matches the date format string.
3. If a match is found, return the date format string.

Python3




test_str = "gfg at 2021-01-04"
 
# Split the input string into words and iterate through them
words = test_str.split()
for word in words:
    if len(word) == 10 and word[4] == "-" and word[7] == "-":
        print(word)
        break


Output

2021-01-04

Time complexity: O(n)
Auxiliary Space: O(1)

METHOD 4:Using Split and Join

APPROACH:

This approach first splits the string into a list of words, then extracts the last word which is the date, and finally splits the date using ‘-‘ and joins it again using ‘-‘.

ALGORITHM:

1.Split the input string by space character, which gives a list of two elements: the text “gfg” and the date string “2021-01-04”.
2.Get the last element of the list (i.e., the date string) using indexing.
3.Split the date string by “-” character, which gives a list of three elements: the year, month, and day.
4.Join the elements of the list with “-” character using the join() method to get the final date string.

Python3




string = 'gfg at 2021-01-04'
date = "-".join(string.split()[-1].split("-"))
print("Computed date:", date)


Output

Computed date: 2021-01-04

Time complexity: O(n), where n is the length of the string.
Space complexity: O(n).

METHOD 5:Using Regular Expression

APPROACH:

The program extracts the date from a given string using regular expression.

ALGORITHM:

1.Import the re module.
2.Define the input string.
3.Use the re.findall() method with a regular expression pattern to extract the date from the string.
4.Print the extracted date.

Python3




import re
 
string = 'gfg at 2021-01-04'
 
date = re.findall('\d{4}-\d{2}-\d{2}', string)[0]
 
print("Computed date:", date)


Output

Computed date: 2021-01-04

Time Complexity: The time complexity of the program depends on the size of the input string and the efficiency of the regular expression pattern. In the worst case, the time complexity is O(n), where n is the length of the input string.

Space Complexity: The space complexity of the program is O(1), as it only stores the extracted date in a variable.

RELATED ARTICLES

Most Popular

Recent Comments