Java初學習 - Regex檢查郵箱有效性__java

來源:互聯網
上載者:User

合法E-mail地址:      1. 必須包含一個並且只有一個符號“@”      2. 第一個字元不得是“@”或者“.”      3. 不允許出現“@.”或者.@      4. 結尾不得是字元“@”或者“.”      5. 允許“@”前的字元中出現“+”      6. 不允許“+”在最前面,或者“+@”           Regex如下:      -----------------------------------------------------------------------      ^(\w+((-\w+)|(\.\w+))*)\+\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$      -----------------------------------------------------------------------           字元描述:      ^ :匹配輸入的開始位置。      \:將下一個字元標記為特殊字元或字面值。      * :匹配前一個字元零次或幾次。      + :匹配前一個字元一次或多次。      (pattern) 與模式比對並記住匹配。      x|y:匹配 x 或 y。      [a-z] :表示某個範圍內的字元。與指定區間內的任何字元匹配。      \w :與任何單詞字元匹配,包括底線。      $ :匹配輸入的結尾。    

//電子郵件      String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";      Pattern regex = Pattern.compile(check);      Matcher matcher = regex.matcher("dffdfdf@qq.com");      boolean isMatched = matcher.matches();      System.out.println(isMatched);                     /* 電話號碼      String check = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$";    Pattern regex = Pattern.compile(check);    Matcher matcher = regex.matcher("13555655606");    boolean isMatched = matcher.matches();    System.out.println(isMatched);      */ 


或使用運算式"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$" 


RegexBuddy的預設檢查Email方式為“\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}\b”


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.