I want to validate usernames according to this schema:
- Allowable characters: letters, numbers, hyphen, underscore
- First character must be a letter or a number
- 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)
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”.