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 |
Este exercício faz parte do curso
Cleaning Data in Java
Instruções do exercício
- Define a regex pattern to match the date format
d/MM/yyyy
. - Check if the entire
date
string matchesdatePattern
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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);
}
}
}