I want to fetch all the email addresses of From field using regex like get all lines of text that starts with "From:" and end with "/n" new line.
Here is the complete text on which I want to apply this regex,
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)
I have tried following patterns but it did not work,
[^.?!]*(?<=[.?\s!])string(?:(?=[\s.?!])[^.?!]*(?:[.?!].*)?)?$
/^([\w\s\.]*)string([\w\s\.]*)$/
"^\\w*\\s*((?m)Name.*$)"
The desired result expected from above text is :
xtest@xyz.co.in, ytest@xyz.com, ytest@xyz.co.in,
PS. I want regex for Java logic
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));
}
output :
xtest@xyz.co.in
ytest@xyz.com
ytest@xyz.co.in
Use this regular expression for your case:
From:\s+([\w-]+@([\w-]+\.)+[\w-]+)
I have tried this regular expression with https://www.freeformatter.com/java-regex-tester.html#ad-output and it is matching what you require.
Your required match is in capture Group 1.
Working Demo: https://regex101.com/r/dGaPbD/4
Try this pattern: ^From:\\s*(\\S+)$
It first matches beginning of a line with ^
, then matches From:
literally, then matches 0 or more whitespaces with \\s*
, then matches one or more non-whitespeaces and stores it in capturing group, $
matches end of a line.
To get e-mail address, just use value of first capturing group.
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.