目的:这是一个通过正则表达式把大家留的邮箱,集中输出。但是,这个正则表达式单独测试一个邮箱正确,但是放到这个程序就读不出来邮箱,这是为什么呢?想不明白!请大家帮帮小弟 谢谢!
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Email { public static void main(String[] args) {
try {
BufferedReader br=new BufferedReader(new FileReader("D:\\workspace\\Email\\email.html"));//通过输入流读取网页文件信息!
String line="";
while((line=br.readLine())!=null){
pase(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} private static void pase(String line) {
Pattern p=Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");//判断邮箱的正则表达式
Matcher m=p.matcher(line);
m.matches();
while(m.find()){
System.out.println(m.group());
}
}}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Email { public static void main(String[] args) {
try {
BufferedReader br=new BufferedReader(new FileReader("D:\\workspace\\Email\\email.html"));//通过输入流读取网页文件信息!
String line="";
while((line=br.readLine())!=null){
pase(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} private static void pase(String line) {
Pattern p=Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");//判断邮箱的正则表达式
Matcher m=p.matcher(line);
m.matches();
while(m.find()){
System.out.println(m.group());
}
}}
pase("[email protected]");
1、什么都没输出。2、pase(line);之前把line打印出来看看。
我全试过了.把line打印出来,是显示的网页的内容.
关键问题在于"matches 方法是尝试将整个输入序列与该模式匹配."