Java regex email

后端 未结 20 2432
清歌不尽
清歌不尽 2020-11-22 13:09

First of all, I know that using regex for email is not recommended but I gotta test this out.

I have this regex:

\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]         


        
20条回答
  •  自闭症患者
    2020-11-22 13:49

    you can use a simple regular expression for validating email id,

    public boolean validateEmail(String email){
      return Pattern.matches("[_a-zA-Z1-9]+(\\.[A-Za-z0-9]*)*@[A-Za-z0-9]+\\.[A-Za-z0-9]+(\\.[A-Za-z0-9]*)*", email)
    }
    

    Description :

    1. [_a-zA-Z1-9]+ - it will accept all A-Z,a-z, 0-9 and _ (+ mean it must be occur)
    2. (\.[A-Za-z0-9]) - it's optional which will accept . and A-Z, a-z, 0-9( * mean its optional)
    3. @[A-Za-z0-9]+ - it wil accept @ and A-Z,a-z,0-9
    4. \.[A-Za-z0-9]+ - its for . and A-Z,a-z,0-9
    5. (\.[A-Za-z0-9]) - it occur, . but its optional

提交回复
热议问题