Bonus Exercise 2 completed.
This commit is contained in:
@@ -27,7 +27,31 @@ public class BonusExercises {
|
|||||||
If no match for a date is found in the string, return null.
|
If no match for a date is found in the string, return null.
|
||||||
*/
|
*/
|
||||||
public String findDate(String string) {
|
public String findDate(String string) {
|
||||||
// todo
|
//Different types of dates
|
||||||
|
String[] datePatterns = {
|
||||||
|
//American: MM/DD/YYYY
|
||||||
|
"\\b(0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3[0-1])/(\\d{4})\\b",
|
||||||
|
|
||||||
|
//British: DD/MM/YYYY
|
||||||
|
"\\b(0[1-9]|1[0-9]|2[0-9]|3[0-1])/(0[1-9]|1[0-2])/(\\d{4})\\b",
|
||||||
|
|
||||||
|
//ISO: YYYY-MM-DD
|
||||||
|
"\\b(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\b",
|
||||||
|
|
||||||
|
//Slash variant: YYYY/MM/DD
|
||||||
|
"\\b(\\d{4})/(0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\b"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String pattern : datePatterns) {
|
||||||
|
Pattern p = Pattern.compile(pattern);
|
||||||
|
Matcher m = p.matcher(string);
|
||||||
|
|
||||||
|
//To return the first date
|
||||||
|
if (m.find()) {
|
||||||
|
return m.group();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user