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存儲過程與函數

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 峨边| 闸北区| 江西省| 徐汇区| 拉孜县| 荆州市| 长岭县| 四子王旗| 荔浦县| 兴业县| 兴山县| 岱山县| 天峻县| 嘉禾县| 潜江市| 寿光市| 乌拉特中旗| 洛南县| 墨玉县| 德州市| 廊坊市| 河源市| 长汀县| 波密县| 宜丰县| 尼玛县| 临清市| 册亨县| 冀州市| 太和县| 安岳县| 工布江达县| 松原市| 荣成市| 新津县| 阳东县| 岑巩县| 古丈县| 高唐县| 建昌县| 乌拉特后旗|