Extracting all present dates in any given String using Regular Expressions
Last Updated : 23 Mar, 2023
Given a string Str, the task is to extract all the present dates from the string. Dates can be in the format i.e., mentioned below:
- DD-MM-YYYY
- YYYY-MM-DD
- DD Month YYYY
Examples:
Input: Str = "The First Version was released on 12-07-2008.The next Release will come on 12 July 2009. The due date for payment is 2023-09-1. India gained its freedom on 15 August 1947 which was a Friday. Republic Day is a public holiday in India where the country marks and celebrates the date on which the Constitution of India came into effect on 26-1-1950."
Output: 12-07-2008
12 July 2009
2023-09-1
15 August 1947
26-1-1950
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = "\\d{2} - \\d{2} - \\d{4}",
"[0 - 9]{2}[/]{1}[0 - 9]{2}[/]{1}[0 - 9]{4}",
"\\d{1, 2} - (January|February|March|April|May|June|July|August|September|October|November|December)-\\d{4}",
"\\d{4} - \\d{1, 2} - \\d{1, 2}",
"[0 - 9]{1, 2}\\s(January|February|March|April|May|June|July|August|September|October|November|December)\\s\\d{4}",
"\\d{1, 2} - \\d{1, 2} - \\d{4}"
Where,
- [d]{2}: This pattern will match two of the preceding items if they are Digits or not.
- |: Either of them should be there
Follow the below steps to implement the idea:
- Create a regex expression to extract all the present dates from the string.
- Use Pattern class to compile the regex formed.
- Use the matcher function to find.
- If it is valid, return true. Otherwise, return false.
Below is the code implementation of the above-discussed approach:
C++ // C++ code for the above approach #include <iostream> #include <regex> using namespace std; // Driver Code int main() { // String containing in it string str = "The First Version was released on 12-07-2008." "Next Release will might come on 12 July 2009. " "The due date for payment is 2023-09-1." "India gained its freedom on 15 August 1947 which was a Friday." "Republic Day is a public holiday in India where the country marks and celebrates " "the date on which the Constitution of India came into effect on 26-1-1950."; // You can Add n number of date // formats in the below given // String Array. string strPattern[] = { "\\d{2}-\\d{2}-\\d{4}", "[0-9]{2}/{1}[0-9]{2}/{1}[0-9]{4}", "\\d{1,2}-(January|February|March|April|May|June|July|August|September|October|November|December)-\\d{4}", "\\d{4}-\\d{1,2}-\\d{1,2}", "[0-9]{1,2}\\s(January|February|March|April|May|June|July|August|September|October|November|December)\\s\\d{4}", "\\d{1,2}-\\d{1,2}-\\d{4}" }; for (int i = 0; i < 6; i++) { regex pattern(strPattern[i]); sregex_iterator matcher(str.begin(), str.end(), pattern); sregex_iterator end; while (matcher != end) { cout << matcher->str() << endl; matcher++; } } return 0; } // This Code is Contributed by Prasad Kandekar(prasad264)
Java // Java code for the above approach import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GFG { // Driver Code public static void main(String[] args) { // String containing in it String str = "The First Version was released on 12-07-2008." + "Next Release will might come on 12 July 2009. " + "The due date for payment is 2023-09-1." + "India gained its freedom on 15 August 1947 which was a Friday." + "Republic Day is a public holiday in India where the country marks and celebrates " + "the date on which the Constitution of India came into effect on 26-1-1950."; // You can Add n number of date // formats in the below given // String Array. String strPattern[] = { "\\d{2}-\\d{2}-\\d{4}", "[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}", "\\d{1, 2}-(January|February|March|April|May|June|July|August|September|October|November|December)-\\d{4}", "\\d{4}-\\d{1, 2}-\\d{1, 2}", "[0-9]{1, 2}\\s(January|February|March|April|May|June|July|August|September|October|November|December)\\s\\d{4}", "\\d{1, 2}-\\d{1, 2}-\\d{4}" }; for (int i = 0; i < strPattern.length; i++) { Pattern pattern = Pattern.compile(strPattern[i]); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group()); } } } }
Python3 # python code for the above approach import re # String containing in it str = "The First Version was released on 12-07-2008." \ "Next Release will might come on 12 July 2009. " \ "The due date for payment is 2023-09-1." \ "India gained its freedom on 15 August 1947 which was a Friday." \ "Republic Day is a public holiday in India where the country marks and celebrates " \ "the date on which the Constitution of India came into effect on 26-1-1950." # You can Add n number of date formats in the below given String Array. str_pattern = [ "\\d{2}-\\d{2}-\\d{4}", "[0-9]{2}/{1}[0-9]{2}/{1}[0-9]{4}", "\\d{1,2}-(January|February|March|April|May|June|July|August|September|October|November|December)-\\d{4}", "\\d{4}-\\d{1,2}-\\d{1,2}", "[0-9]{1,2}\\s(January|February|March|April|May|June|July|August|September|October|November|December)\\s\\d{4}", "\\d{1,2}-\\d{1,2}-\\d{4}" ] for pattern in str_pattern: for match in re.finditer(pattern, str): print(match.group()) # This Code is Contributed by Prasad Kandekar(prasad264)
C# // C# code for the above approach using System; using System.Text.RegularExpressions; class GFG { static void Main(string[] args) { // String containing in it string str = "The First Version was released on 12-07-2008." + "Next Release will might come on 12 July 2009. " + "The due date for payment is 2023-09-1." + "India gained its freedom on 15 August 1947 which was a Friday." + "Republic Day is a public holiday in India where the country marks and celebrates " + "the date on which the Constitution of India came into effect on 26-1-1950."; // You can Add n number of date // formats in the below given // String Array. string[] strPattern = new string[] { "\\d{2}-\\d{2}-\\d{4}", "[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}", "\\d{1, 2}-(January|February|March|April|May|June|July|August|September|October|November|December)-\\d{4}", "\\d{4}-\\d{1, 2}-\\d{1, 2}", "[0-9]{1, 2}\\s(January|February|March|April|May|June|July|August|September|October|November|December)\\s\\d{4}", "\\d{1, 2}-\\d{1, 2}-\\d{4}" }; foreach (string pattern in strPattern) { MatchCollection matches = Regex.Matches(str, pattern); foreach (Match match in matches) { Console.WriteLine(match.Value); } } } }
JavaScript // JavaScript code for the above approach // String containing in it const str = "The First Version was released on 12-07-2008." + "Next Release will might come on 12 July 2009. " + "The due date for payment is 2023-09-1." + "India gained its freedom on 15 August 1947 which was a Friday." + "Republic Day is a public holiday in India where the country marks and celebrates " + "the date on which the Constitution of India came into effect on 26-1-1950."; // You can Add n number of date // formats in the below given // String Array. const strPattern = [ "\\d{2}-\\d{2}-\\d{4}", "[0-9]{2}/{1}[0-9]{2}/{1}[0-9]{4}", "\\d{1,2}-(January|February|March|April|May|June|July|August|September|October|November|December)-\\d{4}", "\\d{4}-\\d{1,2}-\\d{1,2}", "[0-9]{1,2}\\s(January|February|March|April|May|June|July|August|September|October|November|December)\\s\\d{4}", "\\d{1,2}-\\d{1,2}-\\d{4}" ]; for (let i = 0; i < 6; i++) { const pattern = new RegExp(strPattern[i], 'g'); let matcher = str.matchAll(pattern); for (const match of matcher) { console.log(match[0]); } } // This code is contributed by Prajwal Kandekar
Output12-07-2008 2023-09-1 12 July 2009 15 August 1947 12-07-2008 26-1-1950
Time Complexity: O(n*m), N is the length of the input string and M is the number of date formats in the strPattern array.
Space Complexity: O(k), where k is the total number of matched dates.
Related Articles:
Similar Reads
Extracting all Email Ids in any given String using Regular Expressions
Given a string str, the task is to extract all the Email ID's from the given string. Example: Input: "Please send your resumes to Hr@[email protected] for any business inquiry please mail us at business@[email protected]"Output: Hr@[email protected]@[email protected] Appr
4 min read
Extracting Repository Name from a Given GIT URL using Regular Expressions
Given a string str, the task is to extract Repository Name from the given GIT URL. Examples: GIT URL can be any of the formats mentioned below: Input: str="git://github.com/book-Store/My-BookStore.git"Output: My-BookStoreExplanation: The Repo Name of the given URL is: My-BookStore Input: str="git@gi
4 min read
Extracting Port Number from a localhost API Request to a Server using Regular Expressions
Given a String test_str as localhost API Request Address, the task is to get the extract port number of the service. Examples: Input: test_str = âhttp://localhost:8109/users/addUsersâOutput: 8109Explanation: Port Number, 8109 extracted. Input: test_str = âhttp://localhost:1337/api/productsâOutput: 1
5 min read
Extract URLs present in a given string
Given a string S, the task is to find and extract all the URLs from the string. If no URL is present in the string, then print "-1". Examples: Input: S = âWelcome to https://www.geeksforgeeks.org Computer Science PortalâOutput: https://www.geeksforgeeks.orgExplanation:The given string contains the U
5 min read
Extracting PAN Number from GST Number Using Regular Expressions
Given a string str in the form of a GST Number, the task is to extract the PAN Number from the given string. General Format of a GST Number: "22AAAAA0000A1Z5" 22: State CodeAAAAA0000A: Permanent Account Number (PAN)1: Entity Number of the same PANZ: Alphabet Z by default5: Checksum digit Examples: I
4 min read
Perl - Extracting Date from a String using Regex
In Perl generally, we have to read CSV (Comma Separated Values) files to extract the required data. Sometimes there are dates in the file name like sample 2014-02-12T11:10:10.csv or there could be a column in a file that has a date in it. These dates can be of any pattern like YYYY-MM-DDThh:mm:ss or
5 min read
Validating Indian currency data using Regular expressions
Given some Indian Currency Data, the task is to check if they are valid or not using regular expressions. Rules for valid Indian Currency Data are: Indian Rupee format: The currency string starts with the Indian Rupee symbol â¹, followed by a comma-separated integer part that can have one to three di
5 min read
Validate Corporate Identification Number (CIN) using Regular Expression
Given some Corporate Identification Number, the task is to check if they are valid or not using regular expressions. Rules for the valid CIN are: CIN is a 21 digits alpha-numeric code.It starts with either alphabet letter U or L.Next five characters are reserved for digits (0-9).Next two places are
6 min read
International Tracking of Exports validation using Regular Expression
Given some International Tracking Of Exports data, the task is to check if they are valid or not using regular expressions. Rules for the valid International Tracking Of Exports are : It is an alphanumeric string containing UpperCase letters and digits.It either follows the pattern 2 Letters(A-Z)+ 9
5 min read
Pension Scheme Tax Reference Number Validation using Regular Expressions
Given some Pension Scheme Tax Reference Numbers, the task is to check if they are valid or not using regular expressions. Rules for the valid Tax Reference Numbers are : It is an alphanumeric string ie., digits(0-9) and Uppercase Alphabet letters(A-Z).It does not contain whitespaces and other specia
6 min read