這是我先前的職位的延續,我的代碼:正則表達式電子郵件驗證
public class Main {
static String theFile = "C:\\Users\\Pc\\Desktop\\textfile.txt";
public static boolean validate(String input) {
boolean status = false;
String REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
status = true;
} else {
status = false;
}
return status;
}
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(theFile));
String line;
int count = 0;
while ((line = br.readLine()) != null) {
String[] arr = line.split("#");
for (int x = 0; x < arr.length; x++) {
if (arr[x].equals(validate(theFile))) {
count++;
}
System.out.println("no of matches " + count);
}
}
} catch (IOException e) {
e.printStackTrace();
}
Main.validate(theFile);
}
}
它顯示的結果: 沒有比賽0 的不匹配0 沒有比賽0 的不匹配0
的,這是我在輸入文件中的文本 [email protected]#[email protected]#[email protected] #[email protected]
我的輸出應該是三隻郵件,因爲... E中的最後一個字符串是不是一個標準的電子郵件格式 我知道我不是想通過(arr[x].validate(theFile)))
寫一封電子郵件驗證之前,請看看這些[有效和無效的電子郵件地址的實例(https://en.wikipedia.org/wiki/Email_address#Examples) – Andreas
的http:// emailregex .com –