從指定的字串中提取Email

來源:互聯網
上載者:User

從指定的字串中提取Email

 

  1. package net.util.email;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class EmailUtil 
  5. {
  6.     /**
  7.      * 從指定的字串中提取Email content 指定的字串
  8.      */
  9.     public static String parse(String content) 
  10.     {
  11.         String email = null;
  12.         if (content == null || content.length() < 1) {
  13.             return email;
  14.         }
  15.         // 找出含有@
  16.         int beginPos;
  17.         int i;
  18.         String token = "@";
  19.         String preHalf = "";
  20.         String sufHalf = "";
  21.         beginPos = content.indexOf(token);
  22.         if (beginPos > -1) {
  23.             // 前項掃描
  24.             String s = null;
  25.             i = beginPos;
  26.             while (i > 0) {
  27.                 s = content.substring(i - 1, i);
  28.                 if (isLetter(s))
  29.                     preHalf = s + preHalf;
  30.                 else
  31.                     break;
  32.                 i--;
  33.             }
  34.             // 後項掃描
  35.             i = beginPos + 1;
  36.             while (i < content.length()) {
  37.                 s = content.substring(i, i + 1);
  38.                 if (isLetter(s))
  39.                     sufHalf = sufHalf + s;
  40.                 else
  41.                     break;
  42.                 i++;
  43.             }
  44.             // 判斷合法性
  45.             email = preHalf + "@" + sufHalf;
  46.             if (isEmail(email)) {
  47.                 return email;
  48.             }
  49.         }
  50.         return null;
  51.     }
  52.     /**
  53.      * 判斷是不是合法Email email Email地址
  54.      */
  55.     private static boolean isEmail(String email) {
  56.         try {
  57.             if (email == null || email.length() < 1 || email.length() > 256) {
  58.                 return false;
  59.             }
  60.             String check = "^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
  61.             Pattern regex = Pattern.compile(check);
  62.             Matcher matcher = regex.matcher(email);
  63.             boolean isMatched = matcher.matches();
  64.             if (isMatched) {
  65.                 return true;
  66.             } else {
  67.                 return false;
  68.             }
  69.         } catch (RuntimeException e) {
  70.             return false;
  71.         } 
  72.     }
  73.     /**
  74.      * 判斷是不是合法字元 c 要判斷的字元
  75.      */
  76.     private static boolean isLetter(String c) 
  77.     {
  78.         boolean result = false;
  79.         if (c == null || c.length() < 0) {
  80.             return false;
  81.         }
  82.         // a-z
  83.         if (c.compareToIgnoreCase("a") >= 0 && c.compareToIgnoreCase("z") <= 0) {
  84.             return true;
  85.         }
  86.         // 0-9
  87.         if (c.compareToIgnoreCase("0") >= 0 && c.compareToIgnoreCase("9") <= 0) {
  88.             return true;
  89.         }
  90.         // . - _
  91.         if (c.equals(".") || c.equals("-") || c.equals("_")) {
  92.             return true;
  93.         }
  94.         return result;
  95.     }
  96. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.