java怎么通过正则表达式提取一个文件里面的所有邮箱?
发布网友
发布时间:2022-04-29 21:08
我来回答
共7个回答
热心网友
时间:2022-04-27 18:34
package org.com.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class ReadTxt {
static int NUM = 231;
static String[] value = new String[NUM];
public static List<String> listFriends(File file) throws InterruptedException {
List<String> listFriends = new ArrayList<String>();
int n =0;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < NUM; i++) {
int beginIndex = line.indexOf(" n=");
int endIndex = line.indexOf(".com");
if(beginIndex>endIndex){
System.out.println("you are wrong!!!!!!");
n=n+1;
//Thread.sleep(3000);
break;
}
if(beginIndex>-1&&endIndex>-1){
value[i] = line.substring(beginIndex, endIndex);
value[i] = value[i].replaceAll("n=", "<!--");
value[i] = value[i]
.replaceAll("e=", "--><email><receiver>");
value[i] = value[i].replaceAll("\"", "");
listFriends.add(value[i] + "@qq.com</receiver></email>");
//line = line.substring(endIndex * 2 - beginIndex + 2);
break;
}
else {
System.out.println("please go on!!!!!!");
//Thread.sleep(3000);
break;
}
}
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listFriends = removeDuplicateObj(listFriends);
System.out.println(n);
return listFriends;
}
public static List<String> removeDuplicateObj(List<String> list) {
Set<String> someSet = new LinkedHashSet<String>(list);
Iterator<String> iterator = someSet.iterator();
List<String> tempList = new ArrayList<String>();
int i = 0;
while (iterator.hasNext()) {
tempList.add(iterator.next().toString());
i++;
}
return tempList;
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
File file = new File(
"C:\\Documents and Settings\\Administrator\\桌面\\tttttttttttttttttt.txt");
List<String> listFriends = new ArrayList<String>();
listFriends = listFriends(file);
for (String str : listFriends) {
System.out.println(str);
}
System.out.println(listFriends.size());
}
}
热心网友
时间:2022-04-27 19:52
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("H:\\emailtest.txt"));
try {
while((bufferedReader.readLine())!=null)
{
//System.out.println(line);
Parseemail(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void Parseemail(String line)
{
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter("H:\\reslut.java"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Pattern pattern = Pattern.compile("[\\w[.-]]+\\@[\\w[.-]]{2,}\\.[\\w[.-]]+");
Matcher matcher = pattern.matcher(line);
while(matcher.find())
{
System.out.println(matcher.group());
String string = new String(matcher.group());
string += "\n";
try {
bufferedWriter.newLine();
bufferedWriter.write(string, 0, string.length());
bufferedWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
热心网友
时间:2022-04-27 21:26
文件内容读成一个字符串
Pattern p=Pattern.compile("[\w]+@[\w]+\.[com|net|cn]");
Matcher m=p.matcher(文件内容);
while(m.find()){
vfp = m.group();
System.out.println(vfp)://打印所有邮箱
}
热心网友
时间:2022-04-27 23:18
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
public class EmailParser {
private final static Pattern emailer = Pattern
.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String txt = FileUtils.readFileToString(new File("你的文件"));
Matcher matchr = emailer.matcher(txt);
while (matchr.find()) {
String email = matchr.group();
System.out.println(email);
}
}
}
热心网友
时间:2022-04-28 01:26
Pattern p = Pattern.compile("[_/.0-9a-z-]+@([0-9a-z][0-9a-z-]+/.)+[a-z]{2,3}");
Matcher m = p.matcher(builderStr);
while (m.find()) {
System.out.println(m.group());
}
热心网友
时间:2022-04-28 03:50
首先逐行读入,
然后
Pattern pa=Pattern.compile("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
Matcher ma = =pa.matcher(strLine);
while(ma.find()){
System.out.println(ma.group());
}
热心网友
时间:2022-04-28 06:32
[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?
用java正则表达式输出一个字符串中所有的email地址,写出完整测试程序...
import java.util.regex.*; public class Tester { public static void main (String[] args) { String str = "sdfsdfsd23232323@baidu.com2323sdf@sdfs.comddd"; String reg = "([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})"; Pattern pattern = Pa...
java,读取一个文件,提取出里面所有邮箱,怎么写规则
import java.util.Set;public class ReadTxt { static int NUM = 231;static String[] value = new String[NUM];public static List<String> listFriends(File file) throws InterruptedException { List<String> listFriends = new ArrayList<String>();int n =0;try { BufferedReader reader = n...
有没有一个通用的正则表达式来验证多个类型的邮箱
邮箱验证 var par = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;这两个都可以,你试...
java中应用正则表达式判断邮箱格式,如果用户输入格式错误,提醒用户重新...
Pattern p = Pattern.compile("a*b");Matcher m = p.matcher("aaaaab");boolean b = m.matches();这是调用顺序 第一行括号里面是正则表达式。至于正则表达式怎么写 ,你直接百度就好了 有规则的 还有很多别人写好的。最后一行如果是true就代表匹配成功 ...
用Java正则表达式来获取一个字符串中<>里面的内容。
String s = "dsadsadas<peter>dsadasdas<lionel>\"www.163.com\"<kenny><>";Pattern p = Pattern.compile("(<[^>]*>)");Matcher m = p.matcher(s);List<String> result=new ArrayList<String>();while(m.find()){ result.add(m.group());} for(String s1:result){ System.out....
请问怎么用java正则表达式提取以下文本中指定的内容
正则表达式是根据规则来匹配文本,你这里只给了一个文本示例,所以无法建立其比较普遍通用的规则。以下是示例代码,可供参考。代码中对每一个你要的数据进行了一个单独匹配,也可以通过一次匹配取出全部的数据。import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test { publi...
java :从控制台输入一个字符串,验证是不是邮箱?
代码如下:import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入一个邮箱地址:");String mail = sc.nextLine();/* 设定邮箱地址的合法规则,合法邮箱地址要求如下: (1)字符必须是英文或...
使用java正则表达式提取文件名问题,怎么解决
用 fname举个例子 String fName =" G:\\Java_Source\\navigation_tigra_menu\\demo1\\img\\lev1_arrow.gif "; fName = fName.trim(); String temp[] = fName.split("\\\"); /**split里面必须是正则表达式,"\\"的作用是对字符串转义*/ //temp[] = [G:, Java_Source...
如何用java编程实现文本文档中某些有用信息的提取?
1. 读取指定文本文件对象。2. 利用过滤器可以把所需信息读取来即可。3. 把读出来的信息输出(持久化)即可。可参考以下代码 package tool;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;//有序列出指定目录下所有...
怎样用java正则表达式去掉<>里面的内容包括<>
String str="123<aaa>456<bbb>";str=str.replaceAll("<.*?>","");System.out.println(str);