I'm working on a project for school in which we must verify the format of a code. The format is 3 capital letters followed by 3 digits where at least 1 digit is not a 0. So ABC001 is valid. ABC000 is not valid.
Where I'm stuck is how I'd write the format to validate the digits so that 000 is invalid while 001-999 is valid. Below is the code, it's currently written so that 000 is valid.
public static boolean validateInvoiceCode(String invoiceCode)
{
return invoiceCode.matches("[A-Z][A-Z][A-Z][0-9][0-9][0-9]");
}
Add a negative lookbehind (?<!...) that blacklists 000:
[A-Z][A-Z][A-Z][0-9][0-9][0-9](?<!000)
Regex101 Demo
You may also want to improve your regex by using quantifiers, and \d for digits:
[A-Z]{3}\\d{3}(?<!000)
Regex101 Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With