Regex For Creating Usernames

A regular expression (called regex) is a way for a programmer to instruct how a program should look for a specified pattern in text and then what it should do when each pattern match is found. Rather than going through each character of a string and doing matches, regex makes life easier for programmers to do search and matches. This sample regex is a good pattern for use in creating usernames.

The regex below means that only alphanumeric (only lowercase letters) are allowed including an underscore and a dot.

^[a-z0-9_.]$

One Response to Regex For Creating Usernames
  1. Mikuso
    February 20, 2009 | 9:25 am

    With Regex like that, your usernames would only be allowed to be one character long.

    Try something like this instead:
    ^[a-z0-9_.]+$