Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to Allow starting letters and then numbers or letters

Tags:

regex

I need regular Expression which should start with letters only and later it can contain numbers or letters.But starting should contain letters only. I have used

^[a-zA-Z]*[a-zA-Z0-9]+$

But it is not working. Can anyone please help me out?

like image 920
Roopesh Pm Avatar asked May 21 '12 07:05

Roopesh Pm


2 Answers

You used the wrong cardinality. Use + (at least one) instead of * (0 or more), as shown below:

^[a-zA-Z]+[a-zA-Z0-9]*$
like image 50
sp00m Avatar answered Nov 13 '22 04:11

sp00m


The regex you are using is pretty good. The problem with this: ^[a-zA-Z]*[a-zA-Z0-9]+$ is that you are using the * operator, which denotes 0 or more repetitions. If you want it to start with a letter, just use this: ^[a-zA-Z][a-zA-Z0-9]*$. This will match a letter an any letters or numbers which might follow.

like image 2
npinti Avatar answered Nov 13 '22 05:11

npinti