Extracting all present dates in any given String using Regular Expressions
import
java.io.*;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
GFG {
public
static
void
main(String[] args)
{
String str
=
"The First Version was released on 12-07-2008."
+
"Next Release will might come on 12 July 2009. "
+
"The due date for paymnet 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."
;
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());
}
}
}
}
import
java.io.*;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
GFG {
public
static
void
main(String[] args)
{
String str
=
"The First Version was released on 12-07-2008."
+
"Next Release will might come on 12 July 2009. "
+
"The due date for paymnet 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."
;
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());
}
}
}
}