I'm trying to create a regular expression that will allow only specific usernames
- Only starts with alphabet.
- Only ends with either number or an alphabet.
- Only allowed - if contains alphabets only or only one dot with alphabets, only one underscore with alphabets.
- Cannot starts with dot or underscore
- Length of username should be only in 4 to 10 char
this is what i am doing - ^(?=[a-zA-Z0-9._]{4,10}$)(?!.*[_.]{2})[^_.].*[^_.]$
CodePudding user response:
This should work for you:
^(?=[a-zA-Z0-9._]{4,10})[a-zA-Z](?:[a-zA-Z] _[a-zA-Z0-9] |[a-zA-Z] \.[a-zA-Z0-9] |[a-zA-Z0-9] )$
CodePudding user response:
If the only problem currently is that it should not start with numbers, you could use one of these patterns that start the match with a char [A-Za-z]
and ends the match with [a-zA-Z0-9]
In the middle there can only be 1 occurrence of either .
or _
^(?=[a-zA-Z0-9._]{4,10}$)(?!.*[_.]{2})[A-Za-z] (?:[_.][a-zA-Z0-9] )?$
Or another variation:
^[a-zA-Z](?=[a-zA-Z0-9._]{3,9}$)[a-zA-Z0-9]*(?:[._][a-zA-Z0-9]*)?[a-zA-Z0-9]$