亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式匹配直到Java中的文本結尾

正則表達式匹配直到Java中的文本結尾

慕工程0101907 2022-07-20 12:14:36
我想使用正則表達式獲取 From 字段的所有電子郵件地址,例如獲取所有以“From:”開頭并以“/n”新行結尾的文本行。這是我要應用此正則表達式的完整文本,Sent: Tue Mar 05 15:42:11 IST 2019From: [email protected]: [email protected]: Re: Foausrnisfseur invadlide (030000000000:3143)Message: ----------------------------Sent: Tue Mar 05 15:40:51 IST 2019From: [email protected]: [email protected]: Foausrnisfseur invadlide (O4562000888885456:3143)Message:This is not right please correctTermes de paiement Foausrnisfseur non spécifiésimpact potentiel: 3 000,00You should write From field with [email protected] not From: field with [email protected] in the columnDate 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 2019From: [email protected]: [email protected]: Foausrnisfseur invadlide (O4562000888885456:3143)我嘗試了以下模式,但沒有奏效,[^.?!]*(?<=[.?\s!])string(?:(?=[\s.?!])[^.?!]*(?:[.?!].*)?)?$    /^([\w\s\.]*)string([\w\s\.]*)$/    "^\\w*\\s*((?m)Name.*$)"上述文本預期的預期結果是:[email protected], [email protected], [email protected],PS。我想要 Java 邏輯的正則表達式
查看完整描述

4 回答

?
慕村225694

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


查看完整回答
反對 回復 2022-07-20
?
郎朗坤

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


查看完整回答
反對 回復 2022-07-20
?
波斯汪

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

    }

輸出 :


 [email protected]

[email protected]

[email protected]


查看完整回答
反對 回復 2022-07-20
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

試試這個模式:^From:\s*(\S+)$

它首先用 匹配行首^,然后匹配From:字面意思,然后用 匹配 0 個或多個空格\s*,然后匹配一個或多個非空格并將其存儲在捕獲組中,$匹配行尾。

要獲取電子郵件地址,只需使用第一個捕獲組的值。

演示


查看完整回答
反對 回復 2022-07-20
  • 4 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號