0
I’m working on a project and I don’t know how to do it. As in the statement, I need to validate the plates with the Mercosul standard, I have a method to validate the old plates of vehicles.
public boolean validaPlaca(String placa) {
boolean result = false;
Pattern pattern = Pattern.compile("[A-Z]{3}-\\d{4}");
Matcher mat = pattern.matcher(placa);
if (!mat.matches()) {
result = false;
} else {
result = true;
}
return result;
}
Now to validate the standard Mercosur plate do not know how to do. I’ve done:
Pattern pattern = Pattern.compile("[A-Z]{3}\d{1}[A-Z]{3}\d{2}");
And also:
Pattern pattern = Pattern.compile("[A-Z]{3}[0-9][A-Z][0-9]{2}");
None worked, remembering that the Mercosur standard is AAA3B11. The first 3 digits are letters, the fourth is a number, the fifth a letter and the sixth and seventh a number.
What is "didn’t work"? Gave an error? Which formats passed validation and should not have passed?
– Sorack
Utilize
^[A-Z]{3}\\d[A-Z]\\d{2}$
. Example: https://ideone.com/Vq8yi8– Valdeir Psr
Opa blz my friend, in fact he does not check, passes straight..... or is accepted anything
– Wesley Souza
Are you sure the problem isn’t at another point in the code? Because the regex is right: https://ideone.com/e97vyf - Maybe the improvement suggested by Valdeir helps, because it ensures that string only has the board and nothing else. And you can also greatly simplify your method
validaPlaca
, see: https://ideone.com/WMwsKW– hkotsubo