Regular Expression in Java for validating username

后端 未结 4 2034
悲&欢浪女
悲&欢浪女 2021-01-12 20:24

I\'m trying the username chains in Java with following rules:

  • Length >=3
  • Valid characters: a-z, A-Z, 0-9, points, dashes and underscores.
4条回答
  •  不要未来只要你来
    2021-01-12 21:05

    BTW, if there is an extra requirement: the starting letter of the username must be a character, you can write

    try {
        if (subjectString.matches("\\b[a-zA-Z][a-zA-Z0-9\\-._]{3,}\\b")) {
            // Successful match 
    
        } else {
            // No match 
    
        }
    } catch (PatternSyntaxException ex) {
        // Invalid regex 
    
    }
    

    Based on an example here.

提交回复
热议问题