Skip to content

Commit 2b94911

Browse files
committed
feat: Add regex validation to check date format (dd/MM/yyyy)
- Implements Java logic using regular expressions to validate a string as a date. - Target format: "dd/MM/yyyy", where: - `[0-3][0-9]` matches day values from 00 to 39 (loose check, but sufficient for basic validation). - `/` is a literal separator. - `[01][0-9]` matches month values from 00 to 19 (again, not strictly bounded to 12). - `[0-9]{4}` ensures the year has exactly 4 digits. - Used `String.matches()` to apply the regex to the input `"01/01/2000"`. 📝 Notes: - This regex is *syntactically* valid for date format but doesn't enforce logical constraints (e.g., February can't have 31 days). - For stricter validation (like checking leap years or correct day-month combinations), use date parsing libraries (e.g., `SimpleDateFormat` or `LocalDate` with exception handling). Signed-off-by: Somesh diwan <[email protected]>
1 parent 0551e82 commit 2b94911

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Section6StringClassPrinting/src/RegularExpressionChallenge2.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
//String d = "01/01/2000"
33
//Logic = "[0-3][0-9]/[01][0-9]/[0-9]{4}
44

5-
public class RegularExpressionChallenge2
6-
{
7-
public static void main(String[] args)
8-
{
5+
public class RegularExpressionChallenge2 {
6+
public static void main(String[] args) {
97
String d = "01/01/2000";
108

119
// Correct regular expression for the date format
@@ -15,6 +13,7 @@ public static void main(String[] args)
1513
System.out.println(d.matches(regex));
1614
}
1715
}
16+
1817
/*
1918
Correct Regular Expression:
2019
@@ -27,4 +26,4 @@ public static void main(String[] args)
2726
/: Matches the literal / character.
2827
2928
[0-9]{4}: Matches a 4-digit year.
30-
*/
29+
*/

0 commit comments

Comments
 (0)