Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to validate string having only lower case, first char must be a letter

Tags:

string

regex

ruby

I want to use the Regex to validate the string containing

  • must contain only lower case letters
  • first char must be a letter.
  • remaining characters must match regex [a-z0-9_.]

I use the /^[\w\s\-.]*$/ to validate the string. But it allows the first character like (._)

like image 698
Mano Avatar asked Sep 18 '25 18:09

Mano


1 Answers

Why don't you just stick to your requirements ?

  • first char must be a lowercase letter: [a-z]
  • remaining characters must match: [a-z0-9_.]

-> your regex: /^[a-z][a-z0-9_.]*$/

like image 167
xbug Avatar answered Sep 20 '25 10:09

xbug