从指定的字符串中提取Email

从指定的字符串中提取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. }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yexianyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值