Full pattern matching
You're preparing a historical analysis of game releases, but your database contains dates in mixed formats. Before you can analyze release timing patterns, you need to identify which dates need reformatting to match your standard MM/DD/YYYY format. You've extracted dates from the video game dataset below.
Game | Platform | Release date | Quantity (millions of units) |
---|---|---|---|
Wii Sports | Wii | 11/19/2006 | 41.49 |
Super Mario Bros. | NES | 11/13/1985 | 29.08 |
Mario Kart Wii | Wii | 4/10/2008 | 15.85 |
This exercise is part of the course
Cleaning Data in Java
Exercise instructions
- Define a regex pattern to match the date format
d/MM/yyyy
. - Check if the entire
date
string matchesdatePattern
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class PatternValidation {
public static void main(String[] args) {
List dates = Arrays.asList("11/19/2006", "11/13/1985", "4/10/2008", "2008-04-10");
// Define a regex pattern to match the date format
Pattern datePattern = ____.____("\\d{1,2}/\\d{1,2}/\\d{4}");
for (String date : dates) {
// Check if entire date string matches datePattern
boolean isValid = datePattern.____(date).____();
System.out.println(date + " is valid: " + isValid);
}
}
}