posts - 33,  comments - 17,  trackbacks - 0
           1  /**
           2  * 從指定的字符串中提取Email
           3  * content 指定的字符串
           4  */

           5 public static String parse(String content) {
           6  String email = null;
           7  if (content==null || content.length()<1{
           8   return email;
           9  }

          10  //找出含有@
          11  int beginPos;
          12  int i;
          13  String token = "@";
          14  String preHalf="";
          15  String sufHalf = "";
          16  
          17  beginPos = content.indexOf(token);
          18  if (beginPos>-1{
          19   //前項掃描
          20   String s = null;
          21   i= beginPos;
          22   while(i>0{
          23    s = content.substring(i-1,i);
          24    if (isLetter(s))
          25     preHalf = s+preHalf;
          26    else
          27     break;
          28    i--;
          29   }

          30   //后項掃描
          31   i= beginPos+1;
          32   while( i<content.length()) {
          33    s = content.substring(i,i+1);
          34    if (isLetter(s))
          35     sufHalf =  sufHalf +s;
          36    else
          37     break;
          38    i++;  
          39   }

          40   //判斷合法性
          41   email = preHalf + "@" + sufHalf;
          42   if (isEmail(email)) {
          43    return email;
          44   }

          45  }

          46  return null;
          47 }

          48 
          49 /**
          50  * 判斷是不是合法Email
          51  * email Email地址
          52  */

          53 public static boolean isEmail(String email) {
          54  try {
          55   if (email==null || email.length()<1 || email.length()>256{
          56    return false;
          57   }

          58   
          59   String check = "^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
          60   Pattern regex = Pattern.compile(check);
          61   Matcher matcher = regex.matcher(email);
          62   boolean isMatched = matcher.matches();
          63   if(isMatched) {
          64    return true;
          65   }
           else {
          66    return false;
          67   }

          68  }
           catch (RuntimeException e) {
          69   return false;
          70  }
           
          71 }

          72 
          73 /**
          74  * 判斷是不是合法字符
          75  * c 要判斷的字符
          76  */

          77 public static boolean isLetter(String c) {
          78  boolean result = false;
          79  
          80  if (c==null || c.length()<0{
          81   return false;
          82  }

          83  //a-z 
          84  if (c.compareToIgnoreCase("a")>=0 && c.compareToIgnoreCase("z")<=0{
          85   return true;
          86  }

          87  //0-9
          88  if (c.compareToIgnoreCase("0")>=0 && c.compareToIgnoreCase("9")<=0{
          89   return true;
          90  }

          91  //. - _
          92  if (c.equals("."|| c.equals("-"|| c.equals("_") ) {
          93   return true;
          94  }

          95  return result; 
          96 }

          97
          posted on 2008-07-23 17:25 scea2009 閱讀(198) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          PL/SQL存儲過程與函數

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 夏邑县| 闽清县| 璧山县| 大名县| 伊春市| 中超| 临沂市| 嘉善县| 中宁县| 重庆市| 武清区| 承德市| 于都县| 嘉禾县| 昭觉县| 精河县| 乌恰县| 石家庄市| 濉溪县| 瓦房店市| 淮南市| 兴城市| 区。| 深水埗区| 九龙坡区| 黄冈市| 杭锦后旗| 交城县| 扶风县| 东宁县| 石屏县| 锡林浩特市| 汉沽区| 临邑县| 台江县| 白沙| 辉南县| 于都县| 三明市| 汶上县| 漾濞|