Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for at least one alphabet and shouldn't allow dot(.)

I have written the regex below but I'm facing an issue:

^[^\.]*[a-zA-Z]+$

As per the above regex, df45543 is invalid, but I want to allow such a string. Only one alphabet character is mandatory and a dot is not allowed. All other characters are allowed.

like image 685
Udaychandra shivade Avatar asked Mar 16 '23 02:03

Udaychandra shivade


1 Answers

Just add the digits as allowed characters:

^[^\.]*[a-zA-Z0-9]+$

See demo

In case you need to disallow dots, and allow at least 1 English letter, then use lookaheads:

^(?!.*\.)(?=.*[a-zA-Z]).+$

(?!.*\.) disallows a dot in the string, and (?=.*[a-zA-Z]) requires at least one English letter.

See another demo

Another scenario is when the dot is not allowed only at the beginning. Then, use

^(?!\.)(?=.*[a-zA-Z]).+$
like image 182
Wiktor Stribiżew Avatar answered Mar 23 '23 15:03

Wiktor Stribiżew