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: [email protected] "
+ " To: [email protected] "
+ " Subject: Re: Foausrnisfseur invadlide (030000000000:3143) "
+ " Message: "
+ " "
+ " "
+ " ---------------------------- "
+ " "
+ " Sent: Tue Mar 05 15:40:51 IST 2019 "
+ " From: [email protected] "
+ " To: [email protected] "
+ " 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 [email protected] "
+ " and not From: field with [email protected] 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: [email protected] "
+ " To: [email protected] "
+ " 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));
}
輸出 :
添加回答
舉報