posts - 70,comments - 408,trackbacks - 0

          package common;

          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          public class GetEmail {

          ?public static int MAXEMAILLEN = 256;
          ?
          ?/**
          ? * 從指定的字符串中提取Email
          ? *
          ? */
          ?public static String parse(String content) {
          ??String email = null;
          ??if (content==null || content.length()<1) {
          ???return email;
          ??}
          ??//找出含有@
          ??int beginPos;
          ??int i;
          ??String token = "@";
          ??String preHalf="";
          ??String sufHalf = "";
          ??
          ??beginPos = content.indexOf(token);
          ??if (beginPos>-1) {
          ???//前項掃描
          ???String s = null;
          ???i= beginPos;
          ???while(i>0) {
          ????s = content.substring(i-1,i);
          ????if (isLetter(s))
          ?????preHalf = s+preHalf;
          ????else
          ?????break;
          ????i--;
          ???}
          ???//后項掃描
          ???i= beginPos+1;
          ???while( i<content.length()) {
          ????s = content.substring(i,i+1);
          ????if (isLetter(s))
          ?????sufHalf =? sufHalf +s;
          ????else
          ?????break;
          ????i++;??
          ???}
          ???//判斷合法性
          ???email = preHalf + "@" + sufHalf;
          ???if (email.length()>MAXEMAILLEN) {
          ????return null;
          ???}
          ??}
          ??return email;
          ?}
          ?/**
          ? * 判斷是不是合法Email
          ? *
          ? */
          ?public static String isEmail(String email) {
          ??try {
          ???
          ???if (email==null || email.length()<1) {
          ????return null;
          ???}
          ???String check = "^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
          ???Pattern regex = Pattern.compile(check);
          ???Matcher matcher = regex.matcher(email);
          ???boolean isMatched = matcher.matches();
          ???if(isMatched) {
          ????return email;
          ???} else {
          ????return null;
          ???}
          ??} catch (RuntimeException e) {
          ???return null;
          ??}
          ?}
          ?/**
          ? * 判斷是不是合法字符
          ? *
          ? */
          ?public static boolean isLetter(String c) {
          ??boolean result = false;
          ??
          ??if (c==null || c.length()<0)
          ???return false;
          ??//a-z?
          ??if (c.compareToIgnoreCase("a")>=0 && c.compareToIgnoreCase("z")<=0)
          ???return true;
          ??//0-9
          ??if (c.compareToIgnoreCase("0")>=0 && c.compareToIgnoreCase("9")<=0)
          ???return true;
          ??//. - _
          ??if (c.equals(".") || c.equals("-") || c.equals("_") )
          ???return true;

          ??return result;?
          ?}?
          }

          posted on 2007-03-28 17:25 我心依舊 閱讀(453) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 乳山市| 芜湖县| 泽普县| 晴隆县| 普安县| 剑川县| 青岛市| 许昌市| 农安县| 南部县| 类乌齐县| 邹平县| 从江县| 伽师县| 岳普湖县| 山东省| 亳州市| 全州县| 阿图什市| 青铜峡市| 岳普湖县| 廊坊市| 鄂尔多斯市| 邢台市| 福鼎市| 凤台县| 延川县| 泌阳县| 林西县| 永善县| 东源县| 侯马市| 武定县| 太湖县| 营山县| 喀喇| 定结县| 如皋市| 谷城县| 黄陵县| 贡嘎县|