Regular Expression in Java for validating username

后端 未结 4 2036
悲&欢浪女
悲&欢浪女 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:06

    Sarkiroka solution is right, but forgot the dash and the point should be escaped.

    You should add as \ to escape it, but mind that in Java the backslash is itself used to escape, so if you are writing the regex in a java file, you should write

    String regex = "[a-zA-Z0-9\\._\\-]{3,}"; 
    

    Note the double back slashes.

提交回复
热议问题