LoslegenKostenlos starten

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.

Diese Übung ist Teil des Kurses

<Kurs>Cleaning Data in Java</Kurs>
Kurs ansehen

Übungsanweisungen

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

Interaktive praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

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);
        }
    }
}
Code bearbeiten und ausführen