正则表达式匹配直到Java中的文本结尾

我想使用正则表达式获取 From 字段的所有电子邮件地址,例如获取所有以“From:”开头并以“/n”新行结尾的文本行。


这是我要应用此正则表达式的完整文本,


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)

我尝试了以下模式,但没有奏效,


[^.?!]*(?<=[.?\s!])string(?:(?=[\s.?!])[^.?!]*(?:[.?!].*)?)?$    

/^([\w\s\.]*)string([\w\s\.]*)$/    

"^\\w*\\s*((?m)Name.*$)"

上述文本预期的预期结果是:


xtest@xyz.co.in, ytest@xyz.com, ytest@xyz.co.in,


PS。我想要 Java 逻辑的正则表达式


慕工程0101907
浏览 144回答 4
4回答

慕村225694

针对您的情况使用此正则表达式:From:\s+([\w-]+@([\w-]+\.)+[\w-]+)我已经用https://www.freeformatter.com/java-regex-tester.html#ad-output尝试了这个正则表达式,它符合您的要求。您需要的比赛在捕获组 1 中。工作演示:https ://regex101.com/r/dGaPbD/4

郎朗坤

&nbsp; &nbsp;String emailRegex = "[^\\s]+"; // Replace with a better one&nbsp; &nbsp; Matcher m = Pattern.compile("(?m)^From:\\s*(" + emailRegex + ")\\s*$").matcher(yourString);&nbsp; &nbsp; List<String> allMatches = new ArrayList<String>();&nbsp; &nbsp; while(m.find())&nbsp; &nbsp; &nbsp; System.out.println(m.group(1));

波斯汪

&nbsp; String test = "&nbsp; &nbsp;Sent: Tue Mar 05 15:42:11 IST 2019&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;From: xtest@xyz.co.in&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;To: akm@xyz.com&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Subject: Re: Foausrnisfseur invadlide (030000000000:3143)&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Message:&nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;----------------------------&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Sent: Tue Mar 05 15:40:51 IST 2019&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;From: ytest@xyz.com&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;To: bpcla@xpanxion.com&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Subject: Foausrnisfseur invadlide (O4562000888885456:3143)&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Message:&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;This is not right please correct&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Termes de paiement Foausrnisfseur non spécifiés&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;impact potentiel: 3 000,00&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;You should write From field with abc@xyz.com&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;and not From: field with abc@xyz.com in the column&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Date détecté: 2019-02-26 12:55:03&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;---- Please do not delete or modify this line. (2423000000000149:3143) ----&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;" + "&nbsp; &nbsp;-------------------------&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;Sent: Tue Mar 05 15:40:51 IST 2019&nbsp; " + "&nbsp; &nbsp;From: ytest@xyz.co.in&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp;To: bpcla@xpanxion.com&nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; Subject: Foausrnisfseur invadlide (O4562000888885456:3143)&nbsp; ";&nbsp; &nbsp; &nbsp; String emailRegex = "[a-zA-Z0-9._%+-]+@[A-Za-z0-9.-]+\\.[a-zA-Z]{2,6}";&nbsp; &nbsp; Pattern pattern = Pattern.compile("From\\:\\s(" + emailRegex + ")");// From\\:\\s same as Form : and () here i added Email Id&nbsp; regex or you also change to (.*\n) but not recommended&nbsp; &nbsp; Matcher match = pattern.matcher(test);&nbsp; &nbsp; while (match.find()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(match.group(1));&nbsp; &nbsp; }输出 :&nbsp;xtest@xyz.co.inytest@xyz.comytest@xyz.co.in

不负相思意

试试这个模式:^From:\s*(\S+)$它首先用 匹配行首^,然后匹配From:字面意思,然后用 匹配 0 个或多个空格\s*,然后匹配一个或多个非空格并将其存储在捕获组中,$匹配行尾。要获取电子邮件地址,只需使用第一个捕获组的值。演示
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java