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) {
          ???//前項(xiàng)掃描
          ???String s = null;
          ???i= beginPos;
          ???while(i>0) {
          ????s = content.substring(i-1,i);
          ????if (isLetter(s))
          ?????preHalf = s+preHalf;
          ????else
          ?????break;
          ????i--;
          ???}
          ???//后項(xiàng)掃描
          ???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 我心依舊 閱讀(456) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 壤塘县| 老河口市| 贡山| 白银市| 南京市| 惠来县| 额济纳旗| 贡觉县| 南康市| 巴东县| 卢龙县| 翁牛特旗| 大洼县| 浦江县| 东宁县| 新巴尔虎左旗| 阳曲县| 开原市| 廊坊市| 通海县| 镇远县| 萨迦县| 台南县| 收藏| 年辖:市辖区| 绥阳县| 鄢陵县| 望奎县| 吉木萨尔县| 柳江县| 巴马| 南丰县| 内江市| 湘西| 浏阳市| 屏山县| 奎屯市| 赣榆县| 旌德县| 城口县| 平江县|