Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usernames that cannot start or end with characters

Tags:

regex

ruby

I'm trying to achieve these rules with a regex:

  • Can contain lowercase and uppercase letters and numbers
  • Can contain underscores and periods
  • Cannot contain 2 underscores in a row
  • Cannot contain 2 periods in a row
  • Cannot begin or end with an underscore or period
  • Cannot contain letters with accents
  • Must be between 3 and 28 letters in length

Valid:

Spicy_Pizza
97Indigos
Infinity.Beyond

Invalid:

_yahoo
powerup.
un__real
no..way

Here's my current regex:

^(?:[a-zA-Z0-9]|([._])(?!\1)){3,28}$

All rules seem to be working other than the exception for starting and ending with underscores or periods.

like image 654
Kobius Avatar asked Aug 13 '18 00:08

Kobius


People also ask

What special characters are allowed in usernames?

Special characters Usernames can contain letters (a-z), numbers (0-9), and periods (.). Usernames cannot contain an ampersand (&), equals sign (=), underscore (_), apostrophe ('), dash (-), plus sign (+), comma (,), brackets (<,>), or more than one period (.) in a row.

How many characters should a username have?

In some (web) applications there is a minimum length for usernames, usually there is a restriction for a minimum of 6 characters length. For example, free gmail accounts and miiverse (Nintendo social network).

Is underscore allowed in password?

underscore "_" is not considered as a special character in password reset #169.


2 Answers

Sounds like you just need to add an alphanumeric check to the first and last character of the string. Because that will take up 2 characters, change the inner repetition from {3,28} to {1,26}:

^[A-Za-z\d](?:[a-zA-Z0-9]|([._])(?!\1)){1,26}[A-Za-z\d]$
 ^^^^^^^^^^                             ^^^^ ^^^^^^^^^^

https://regex101.com/r/G6bVaZ/1

like image 176
CertainPerformance Avatar answered Oct 05 '22 23:10

CertainPerformance


I prefer making clear that the string cannot begin or end with a period or underscore, as opposed to stipulating which characters are permitted at the beginning and end of the string.

r = /
    \A            # match beginning of string
    (?![._])      # next char cannot be a period or underscore
    (?:           # begin non-capture group
      [a-zA-Z0-9] # match one of chars in indicated
      |           # or
      ([._])      # match a period or underscore in capture group 1
      (?!\1)      # next char cannot be the contents of capture group 1
    ){3,28}       # end non-capture group and execute non-capture group 3-28 times
    (?<![._])     # previous char cannot be a period or underscore
    \z            # match end of string
    /x            # free-spacing regex definition mode

%w| Spicy_Pizza 97Indigos Infinity.Beyond _yahoo powerup. un__real no..way |.each do |s|
  puts "%s: %s" % [s, s.match?(r) ? "valid" : "invalid"]
end

Spicy_Pizza:     valid
97Indigos:       valid
Infinity.Beyond: valid
_yahoo:          invalid
powerup.:        invalid
un__real:        invalid
no..way:         invalid
like image 27
Cary Swoveland Avatar answered Oct 06 '22 00:10

Cary Swoveland