Python - Extract date in String
Last Updated :
23 Apr, 2023
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))
OutputThe 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
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)
OutputComputed 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)
OutputComputed 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.
Similar Reads
Python - Validate String date format Given a date format and a string date, the task is to write a python program to check if the date is valid and matches the format.Examples:Input : test_str = '04-01-1997', format = "%d-%m-%Y"Output : TrueExplanation : Formats match with date.Input : test_str = '04-14-1997', format = "%d-%m-%Y"Output
3 min read
Python Extract Substring Using Regex Python provides a powerful and flexible module called re for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple a
2 min read
Convert string to DateTime and vice-versa in Python A common necessity in many programming applications is dealing with dates and times. Python has strong tools and packages that simplify handling date and time conversions. This article will examine how to effectively manipulate and format date and time values in Python by converting Strings to Datet
6 min read
Convert Epoch Time to Date Time in Python Epoch time, also known as Unix time or POSIX time, is a way of representing time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Converting epoch time to a human-readable date and time is a common task in programming, especially i
3 min read
Extract List of Substrings in List of Strings in Python Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c
3 min read
Convert "unknown format" strings to datetime objects in Python In this article, we are going to see how to convert the "Unknown Format" string to the DateTime object in Python. Suppose, there are two strings containing dates in an unknown format and that format we don't know. Here all we know is that both strings contain valid date-time expressions. By using th
3 min read