Valid Username Regular Expression

  • + 39 comments
    String regex = "^[aA-zZ]\\w{7,29}$";
    

    If someone manages to reach my comment, let me explain, in detail, why this works. ^ represents that starting character of the string. [aA-zZ] makes sure that the starting character is in the lowercase or uppercase alphabet. \\w{7,29} represents a check to make sure that the remaining items are word items, which includes the underscore, until it reaches the end and that is represented with $. The {7,29} represents the 8-30 character constraint given to us minus the predefined first character.