IniziaInizia gratis

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

The necessary packages such as Pattern, and Matcher have been imported for you.

Questo esercizio fa parte del corso

Cleaning Data in Java

Visualizza il corso

Istruzioni dell'esercizio

  • Define a regex pattern to match the date format d/MM/yyyy.
  • Check if the entire date string matches datePattern.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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);
        }
    }
}
Modifica ed esegui il codice