자바(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));
}
}
}
출처: