Regex help needed for user registration system?

Viewed 85

I am trying to create a regex for user registration, wherein the length is greater than 6, where the passed string can have letters, numbers but only 1 hyphen and no other symbol. Also, the username should not end with a hypen and always start with a letter.

So far, I have done this:

^[a-z A-Z]+(-[a-z A-Z]+)?$
3 Answers

I believe this could work:

^(?!(.*-.*){2,})(?!.*-$)[a-zA-Z][-a-zA-Z0-9]{6,}$

These are the parts:

  • ^ matches the beginning of the string.
  • (?!(.*-.*){2,}) is a negative lookahead that checks if there are more than two dashes.
  • (?!.*-$) is a negative lookahead that checks if the string ends with a dash.
  • [a-zA-Z] matches a lowercase or uppercase letter as the first character of the string.
  • [-a-zA-Z0-9]{6,} matches at least 6 characters which can include dashes, lowercase letters, uppercase letters or digits. These 6 characters complete at least 7 characters in the string including the first letter described in the part right above.
  • $ matches the end of the string.

You might use

^(?=[a-zA-Z0-9]{7})[a-zA-Z][a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)?$

Explanation

  • ^ Start of string
  • (?=[a-zA-Z0-9]{7}) Positive lookahead, assert 7 chars at the right (greater than 6, or change to your requirements)
  • [a-zA-Z] Match a char a-zA-Z
  • [a-zA-Z0-9]+ Match 1+ times a char a-zA-Z0-9
  • (?: Non capture group
    • -[a-zA-Z0-9]+ Match a single hyphen and 1+ times any of the listed
  • )? Close group and make it optional
  • $ End of string

Regex demo

Use

^(?=[a-zA-Z].{6})[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)?$

See proof. (?=[a-zA-Z].{6}) makes sure the name starts with a letter and then has at least 6 chars. [a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)? allows letters and digits, and only one optional hyphen, with no hyphen at the start and end.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [a-zA-Z]                 any character of: 'a' to 'z', 'A' to 'Z'
--------------------------------------------------------------------------------
    .{6}                     any character except \n (6 times)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-Z0-9]+             any character of: 'a' to 'z', 'A' to 'Z',
                           '0' to '9' (1 or more times (matching the
                           most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
    [a-zA-Z0-9]+             any character of: 'a' to 'z', 'A' to
                             'Z', '0' to '9' (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Related