I'm using this simple regular expression to validate a hex string:
^[A-Fa-f0-9]{16}$
As you can see, I'm using a quantifier to validate that the string is 16 characters long. I was wondering if I can use another quantifier in the same regex to validate the string length to be either 16 or 18 (not 17).
I believe
^([A-Fa-f0-9]{2}){8,9}$
will work.
This is nice because it generalizes to any even-length string.
That's just a 16 character requirement with an optional 2 character afterwards:
^[A-Fa-f0-9]{16}([A-Fa-f0-9]{2})?$
The parentheses may not be required - I'm not enough of a regex guru to know offhand, I'm afraid. If anyone wants to edit it, do feel free...
^(?:[A-Fa-f0-9]{16}|[A-Fa-f0-9]{18})$
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