Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex that expresses "at least one non-digit"

Tags:

regex

ruby

I want to validate usernames according to this schema:

  1. Allowable characters: letters, numbers, hyphen, underscore
  2. First character must be a letter or a number
  3. The username cannot be all numbers

This regular expression satisfies 1 and 2 above, but I can't figure out how to satisfy 3:

/^[a-zA-Z\d][\w\-]+$/

(I'm using Ruby, if that's relevant)

like image 908
Tom Lehman Avatar asked Jan 24 '10 04:01

Tom Lehman


1 Answers

Not very efficient, but simple:

/^(?!\d+$)[a-zA-Z\d][\w\-]+$/

The lookahead simply means: "what follows isn't a string of numbers that go on until the end".

like image 80
Max Shawabkeh Avatar answered Sep 21 '22 05:09

Max Shawabkeh