java正则邮箱
㈠ java 带要求的验证邮箱的正则表达式
//一般用于帐号(Email)
validator.entity.setRuleEmail("^(\\w|\\.|-|\\+)+@(\\w|-)+(\\.(\\w|-)+)+$");
贴上完整的邮箱验证代码:
/**
* 验证email规则
*
* @param email
* @return
*/
publicbooleanvalidateEmail(String email)
{
return TextUtils.isEmpty(email)? false
:email.matches(entity.ruleEmail);
}
㈡ java怎么通过正则表达式提取一个文件里面的所有邮箱
package org.com.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class ReadTxt {
static int NUM = 231;
static String[] value = new String[NUM];
public static List<String> listFriends(File file) throws InterruptedException {
List<String> listFriends = new ArrayList<String>();
int n =0;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < NUM; i++) {
int beginIndex = line.indexOf(" n=");
int endIndex = line.indexOf(".com");
if(beginIndex>endIndex){
System.out.println("you are wrong!!!!!!");
n=n+1;
// Thread.sleep(3000);
break;
}
if(beginIndex>-1&&endIndex>-1){
value[i] = line.substring(beginIndex, endIndex);
value[i] = value[i].replaceAll("n=", "<!--");
value[i] = value[i]
.replaceAll("e=", "--><email><receiver>");
value[i] = value[i].replaceAll("\"", "");
listFriends.add(value[i] + "@qq.com</receiver></email>");
// line = line.substring(endIndex * 2 - beginIndex + 2);
break;
}
else {
System.out.println("please go on!!!!!!");
// Thread.sleep(3000);
break;
}
}
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listFriends = removeDuplicateObj(listFriends);
System.out.println(n);
return listFriends;
}
public static List<String> removeDuplicateObj(List<String> list) {
Set<String> someSet = new LinkedHashSet<String>(list);
Iterator<String> iterator = someSet.iterator();
List<String> tempList = new ArrayList<String>();
int i = 0;
while (iterator.hasNext()) {
tempList.add(iterator.next().toString());
i++;
}
return tempList;
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
File file = new File(
"C:\\Documents and Settings\\Administrator\\桌面\\tttttttttttttttttt.txt");
List<String> listFriends = new ArrayList<String>();
listFriends = listFriends(file);
for (String str : listFriends) {
System.out.println(str);
}
System.out.println(listFriends.size());
}
}
㈢ 利用Java的字符串处理技术,编写一个验证Email地址合法性的程序
实现原理,实际上就是输入一个字符串,之后通过正则表达式验证此字符串是否符合邮箱的格式规范,通过以下工具类即可:
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassRegisterCheck{
/**
*验证输入的邮箱格式是否符合
*@paramemail
*@return是否合法
*/
(Stringemail)
{
booleantag=true;
finalStringpattern1="^([a-z0-9A-Z]+[-|//.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?//.)+[a-zA-Z]{2,}$";
finalPatternpattern=Pattern.compile(pattern1);
finalMatchermat=pattern.matcher(email);
if(!mat.find()){
tag=false;
}
returntag;
}
}
㈣ java 验证邮箱格式怎么实现
public class Test{
public static void main(String[] args){
Test t = new Test();
String email = "[email protected]" ;
boolean isEmail = t.isEmail(email);
if(isEmail){
System.out.println(email +" is a right email!!!");
}else{
System.out.println("error");
}
}
public boolean isEmail(String email){
//正则表达式
/*
String regex = "^[A-Za-z]{1,40}@[A-Za-z0-9]{1,40}\\.[A-Za-z]{2,3}$";
return email.matches(regex);
*/
//不适用正则
if(email==null||"".equals(email)) return false ;
if(!containsOneWord('@',email)||!containsOneWord('.',email)) return false;
String prefix = email.substring(0,email.indexOf("@"));
String middle = email.substring(email.indexOf("@")+1,email.indexOf("."));
String subfix = email.substring(email.indexOf(".")+1);
System.out.println("prefix="+prefix +" middle="+middle+" subfix="+subfix);
if(prefix==null||prefix.length()>40||prefix.length()==0) return false ;
if(!isAllWords(prefix)) return false ;
if(middle==null||middle.length()>40||middle.length()==0) return false ;
if(!isAllWordsAndNo(middle)) return false ;
if(subfix==null||subfix.length()>3||subfix.length()<2) return false ;
if(!isAllWords(subfix)) return false ;
return true ;
}
//判断字符串只包含指定的一个字符c
private boolean containsOneWord(char c , String word){
char[] array = word.toCharArray();
int count = 0 ;
for(Character ch : array){
if(c == ch) {
count++;
}
}
return count==1 ;
}
//检查一个字符串是否全部是字母
private boolean isAllWords(String prefix){
char[] array = prefix.toCharArray();
for(Character ch : array){
if(ch<'A' || ch>'z' || (ch<'a' && ch>'Z')) return false ;
}
return true;
}
//检查一个字符串是否包含字母和数字
private boolean isAllWordsAndNo(String middle){
char[] array = middle.toCharArray();
for(Character ch : array){
if(ch<'0' || ch > 'z') return false ;
else if(ch >'9' && ch <'A') return false ;
else if(ch >'Z' && ch <'a') return false ;
}
return true ;
}
}
㈤ Java中的邮箱地址的规则:只能包含一个@符号,在@符号后必须由.符号…
public static void main(String[] args) throws IOException {
// 要验证的字符串
String str = "[email protected]";
// 邮箱验证规则
String regex = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
boolean rs = matcher.matches();
System.out.println(rs);
}