从指定的字符串中提取Email
- packagenet.util.email;
- importjava.util.regex.Matcher;
- importjava.util.regex.Pattern;
- publicclassEmailUtil
- {
- /**
- *从指定的字符串中提取Emailcontent指定的字符串
- */
- publicstaticStringparse(Stringcontent)
- {
- Stringemail=null;
- if(content==null||content.length()<1){
- returnemail;
- }
- //找出含有@
- intbeginPos;
- inti;
- Stringtoken="@";
- StringpreHalf="";
- StringsufHalf="";
- beginPos=content.indexOf(token);
- if(beginPos>-1){
- //前项扫描
- Strings=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(isEmail(email)){
- returnemail;
- }
- }
- returnnull;
- }
- /**
- *判断是不是合法EmailemailEmail地址
- */
- privatestaticbooleanisEmail(Stringemail){
- try{
- if(email==null||email.length()<1||email.length()>256){
- returnfalse;
- }
- Stringcheck="^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
- Patternregex=Pattern.compile(check);
- Matchermatcher=regex.matcher(email);
- booleanisMatched=matcher.matches();
- if(isMatched){
- returntrue;
- }else{
- returnfalse;
- }
- }catch(RuntimeExceptione){
- returnfalse;
- }
- }
- /**
- *判断是不是合法字符c要判断的字符
- */
- privatestaticbooleanisLetter(Stringc)
- {
- booleanresult=false;
- if(c==null||c.length()<0){
- returnfalse;
- }
- //a-z
- if(c.compareToIgnoreCase("a")>=0&&c.compareToIgnoreCase("z")<=0){
- returntrue;
- }
- //0-9
- if(c.compareToIgnoreCase("0")>=0&&c.compareToIgnoreCase("9")<=0){
- returntrue;
- }
- //.-_
- if(c.equals(".")||c.equals("-")||c.equals("_")){
- returntrue;
- }
- returnresult;
- }
- }