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

时间:2019-03-05 12:41:05

标签: java arrays regex string collections

我想使用正则表达式获取“发件人”字段的所有电子邮件地址,例如获取以“发件人:”开头并以“ / 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逻辑的正则表达式

4 个答案:

答案 0 :(得分:0)

  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

答案 1 :(得分:0)

针对您的情况使用此正则表达式:

From:\s+([\w-]+@([\w-]+\.)+[\w-]+)

我用https://www.freeformatter.com/java-regex-tester.html#ad-output尝试过此正则表达式,它符合您的要求。

您所需的匹配项在捕获组1中。

正在运行的演示:https://regex101.com/r/dGaPbD/4

答案 2 :(得分:0)

尝试以下模式:^From:\s*(\S+)$

它首先与^匹配一行的开始,然后按字面匹配From:,然后与\s*匹配0个或多个空格,然后匹配一个或多个非白空格并将其存储在捕获组中,$与行尾匹配。

要获取电子邮件地址,只需使用第一个捕获组的值即可。

Demo

答案 3 :(得分:0)

    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));