Valid Username Regular Expression

  • + 2 comments

    Actually, the contents of [...] are interpreted as literal characters, so your regex will not only match alphanumeric characters, but also [, |, etc.

    Use something like this:

    String pattern = "^[A-ZA-z]\\w{7,29}$"
    

    [A-Za-z] matches any uppercase or lowercase letter. \\w matches any alphanumeric or _ character.