티스토리 뷰

자바(Java)

자바 이메일 추출

xemaker 2020. 7. 7. 15:19
package zzz;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class foo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		HashSet<String> hs = new HashSet<>();
		FileReader file = null;
		try {
			file = new FileReader(new File("경로"));
		} catch (FileNotFoundException e1) {
			System.err.println("File emails.txt not found!");
			e1.printStackTrace();
		}
		BufferedReader br = new BufferedReader(file);
		String line;
		try {
			while ((line = br.readLine()) != null) {
				fillEmailsHashSet(line, hs);
			}

		} catch (IOException e) {
			System.err.println("Error when reading");
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					System.err.println("Unexpected error");
					e.printStackTrace();
				}
			}
		}
		for (String string : hs) {
			System.out.println(string);
		}
	}

	public static void fillEmailsHashSet(String line,HashSet<String> container){

        Pattern p = Pattern.compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");
        Matcher m = p.matcher(line);

        while(m.find()) {
            container.add(m.group(1));
        }

    }

}

출처:

https://okky.kr/article/279926

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함