當前位置:首頁 » 編程語言 » java正則郵箱

java正則郵箱

發布時間: 2023-12-26 01:48:12

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);
}

熱點內容
安卓怎麼充高會 發布:2024-11-30 11:29:49 瀏覽:656
什麼是手機本地伺服器ip 發布:2024-11-30 11:13:21 瀏覽:288
壓縮袋無泵 發布:2024-11-30 11:13:11 瀏覽:676
去角質皮面膜怎麼樣配置 發布:2024-11-30 10:44:22 瀏覽:808
證券首次開戶後為什麼沒密碼 發布:2024-11-30 10:41:57 瀏覽:316
玩具廠資料庫 發布:2024-11-30 10:41:57 瀏覽:786
學校考試伺服器地址 發布:2024-11-30 10:35:30 瀏覽:683
nas無盤伺服器搭建教程 發布:2024-11-30 10:27:07 瀏覽:156
觸摸精靈腳本解密 發布:2024-11-30 10:27:04 瀏覽:328
如何解鎖密碼鎖上的密碼用數字解 發布:2024-11-30 10:07:55 瀏覽:454