4 回答
TA贡献1880条经验 获得超4个赞
针对您的情况使用此正则表达式:
From:\s+([\w-]+@([\w-]+\.)+[\w-]+)
我已经用https://www.freeformatter.com/java-regex-tester.html#ad-output尝试了这个正则表达式,它符合您的要求。
您需要的比赛在捕获组 1 中。
工作演示:https ://regex101.com/r/dGaPbD/4
TA贡献1921条经验 获得超9个赞
String emailRegex = "[^\\s]+"; // Replace with a better one
Matcher m = Pattern.compile("(?m)^From:\\s*(" + emailRegex + ")\\s*$").matcher(yourString);
List<String> allMatches = new ArrayList<String>();
while(m.find())
System.out.println(m.group(1));
TA贡献1811条经验 获得超4个赞
String test = " Sent: Tue Mar 05 15:42:11 IST 2019 "
+ " From: xtest@xyz.co.in "
+ " To: akm@xyz.com "
+ " Subject: Re: Foausrnisfseur invadlide (030000000000:3143) "
+ " Message: "
+ " "
+ " "
+ " ---------------------------- "
+ " "
+ " Sent: Tue Mar 05 15:40:51 IST 2019 "
+ " From: ytest@xyz.com "
+ " To: bpcla@xpanxion.com "
+ " Subject: Foausrnisfseur invadlide (O4562000888885456:3143) "
+ " Message: "
+ " This is not right please correct "
+ " Termes de paiement Foausrnisfseur non spécifiés "
+ " impact potentiel: 3 000,00 "
+ " You should write From field with abc@xyz.com "
+ " and not From: field with abc@xyz.com in the column "
+ " Date détecté: 2019-02-26 12:55:03 "
+ " "
+ " "
+ " ---- Please do not delete or modify this line. (2423000000000149:3143) ---- "
+ " " + " ------------------------- "
+ " Sent: Tue Mar 05 15:40:51 IST 2019 " + " From: ytest@xyz.co.in "
+ " To: bpcla@xpanxion.com "
+ " Subject: Foausrnisfseur invadlide (O4562000888885456:3143) ";
String emailRegex = "[a-zA-Z0-9._%+-]+@[A-Za-z0-9.-]+\\.[a-zA-Z]{2,6}";
Pattern pattern = Pattern.compile("From\\:\\s(" + emailRegex + ")");// From\\:\\s same as Form : and () here i added Email Id regex or you also change to (.*\n) but not recommended
Matcher match = pattern.matcher(test);
while (match.find()) {
System.out.println(match.group(1));
}
输出 :
xtest@xyz.co.in
ytest@xyz.com
ytest@xyz.co.in
添加回答
举报