How can I use this pattern: [A-Za-z0-9]{10}
to also match to other text sizes like: 12 and 25?
I tried to make it like:
[A-Za-z0-9]{10|12|25}
([A-Za-z0-9]){10}|{12}|{25}
But it didn't succeed.
You need to use alternations if you need to match specific sized only:
^(?:[A-Za-z0-9]{10}|[A-Za-z0-9]{12}|[A-Za-z0-9]{25})$
If you want to match symbols within a range, say, from 10 to 25, you can use
^[A-Za-z0-9]{10,25}$
Also, [A-Za-z0-9]
can be replaced with \p{Alnum}
(see Java regex reference).
\p{Alnum}
An alphanumeric character:[\p{Alpha}\p{Digit}]
Java code demo with String#matches
(that does not require anchors):
System.out.println("1234567890".matches("[A-Za-z0-9]{10}|[A-Za-z0-9]{12}|[A-Za-z0-9]{25}"));
// => true, 10 Alnum characters
System.out.println("12345678901".matches("\\p{Alnum}{10}|\\p{Alnum}{12}|\\p{Alnum}{25}"));
// => false, 11 Alnum characters
System.out.println("123456789012".matches("\\p{Alnum}{10}|\\p{Alnum}{12}|\\p{Alnum}{25}"));
// => true, 12 Alnum characters
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