本文利用的工具是eclipse
首先先在网络上选取一个具有所抓包的htm文件
然后编写代码
import java.io.IOException;
import java.util.regex.*;
import java.io.FileReader;
import java.io.BufferedReader;
public class MyRegex {
public static void main(String args[]){
try{
String content = read("d://7.htm");//这个要看你自己保存的文件而进行修改
String regex="http://[\\w[.-]]+[\\w[.-]]+\\.[\\w]+" ; //此为正则表达式
Matcher m=Pattern.compile(regex).matcher(content);
while(m.find()){
System.out.println(m.group());
}
}catch(IOException e){
e.printStackTrace();
}
}
public static String read(String fileName)throws IOException{
BufferedReader br =new BufferedReader(new FileReader(fileName));
StringBuilder sb=new StringBuilder();
String s;
while((s=br.readLine())!=null){
sb.append(s);
sb.append("/r/n");
}
br.close();
return sb.toString();
}
}
正则表达式:
抓邮箱 "\\w+([.+-']\\w+)*@\\w+([.-]\\w+)*\\.\\w+([.-]\\w+)*"
抓超链接 "http://\\w+([-+.']\\w+)*\\.\\w+([-.]\\w+)*"