这是我先前的职位的延续,我的代码:正则表达式电子邮件验证
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 –