When working with license plate data, you may sometimes need to determine if a given license plate number matches a specific pattern. This can be useful for validating license plate entries or filtering them based on specific criteria. In Java, you can use regular expressions to achieve this.
Here’s an example that demonstrates how to match license plate patterns using Java regular expressions:
import java.util.regex.*;
public class LicensePlateMatcher {
public static void main(String[] args) {
// License plate patterns
String pattern1 = "\\d{4}"; // Matches 4 digits
String pattern2 = "[A-Z]{3}\\d{3}"; // Matches 3 uppercase letters followed by 3 digits
String pattern3 = "[A-Z]{2}-\\d{4}"; // Matches 2 uppercase letters, a hyphen, and 4 digits
// License plate numbers to match
String[] licensePlates = {
"1234",
"ABC123",
"AB-1234",
"XYZ-9876"
};
// Match license plate patterns
for (String plate : licensePlates) {
if (Pattern.matches(pattern1, plate)) {
System.out.println(plate + " matches pattern 1");
} else if (Pattern.matches(pattern2, plate)) {
System.out.println(plate + " matches pattern 2");
} else if (Pattern.matches(pattern3, plate)) {
System.out.println(plate + " matches pattern 3");
} else {
System.out.println(plate + " does not match any pattern");
}
}
}
}
In this example, we define three license plate patterns using regular expressions. The patterns are as follows:
\\d{4}
matches 4 digits, e.g.,1234
.[A-Z]{3}\\d{3}
matches 3 uppercase letters followed by 3 digits, e.g.,ABC123
.[A-Z]{2}-\\d{4}
matches 2 uppercase letters, a hyphen, and 4 digits, e.g.,AB-1234
.
We then create an array of license plate numbers to match against the defined patterns. The Pattern.matches()
method is used to check if a license plate number matches a specific pattern. Finally, we print the appropriate message based on the matched pattern.
This approach allows you to easily match license plate numbers against specific patterns using Java regular expressions. Feel free to modify the patterns or add more complex ones to suit your specific needs.
#java #regularExpressions