- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.regex.*;
- public class MyRegex {
- public static void main(String[] args) {
- try {
- //读取网页文件,里面有N个邮箱地址
- String content = read("e:/1.htm");//read()方法在下面
- //匹配邮箱的正则表达式
- String regex="//w+([-+.']//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*";
- Matcher m=Pattern.compile(regex).matcher(content);
- while(m.find()){
- //输出邮箱地址
- System.out.println(m.group());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取文本内容
- * @param fileName 文本名称,包括路径
- * @return 返回文本内容
- */
- 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();
- }
- }
用正则表达式提取网页中的邮箱地址
最新推荐文章于 2023-05-06 11:43:35 发布