Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx to match alphanumeric number of certain length starting with a number

Tags:

java

regex

I have a requirement for regex - starting with a number and then can have letter numbers and special characters -_.\\s+ for hyphen, dot, underscore and space, etc. My other important requirement is that the length of the number is to be between 5 and 30 (inclusive) BUT it should not be 10 or 12 characters in length. I have prepared a regex as below:

^[0-9][a-zA-Z0-9-_.\\\s+]{5,30}$

I understand that the above regex regulates length between 5 and 30 characters BUT how do I restrict it NOT to have 10 or 12 characters length.

Please help.

like image 595
sidd Avatar asked Jan 01 '26 19:01

sidd


1 Answers

BUT how do I restrict it NOT to have 10 or 12 characters length.

You could insert a negative lookahead at the start of the pattern to not expect 10 or 12 characters: (?!.{10}$|.{12}$).

The whole pattern would then be:

^(?!.{10}$|.{12}$)[0-9][a-zA-Z0-9-_.\\\s+]{5,30}$
like image 76
GOTO 0 Avatar answered Jan 04 '26 12:01

GOTO 0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!