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
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.
Demo
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
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));
Related
I have a String that looks like this:
String meta = "1 \n"
+ "Herst \n"
+ "01 Jan 2019 – 31 Dec 2020 \n"
+ "01 Jan 2020 \n"
+ "CONFIG \n"
+ "XML \n"
+ "AES \n"
+ "RSA \n"
+ "256 \n"
+ "16 \n"
+ "128 \n";
What is the smartest way if I want to read a specific line out of this String in Java?
For example, I need in another part of my code the number of the second last line (in this case it's 16). How can I read this number out of the String?
If it's already in String form, just split it into lines using \n as a delimiter to get an array of lines:
String[] lines = meta.split("\n");
Then you can easily get a specific line. For instance, System.out.println(lines[9]) will print 16.
If you need the 16 in the form of an int, you'd need to remove the whitespaces around it, and parse it:
int parsed = Integer.parseInt(lines[9].trim());
I have the following expression:
(?!\d+\s+TOTAL\s+)\$+\d+\.?\d+\s+
It produces the result "$23.00$0.03$23.80" from the following text:
SPEEDWAY 3007906
Wallace NC 28466
TRAM: 1086244
9/17/2017 2:12 pm
Pump 08
Regular Unleaded
8,716 # $2,639/6131
GAS TOTAL $23.00
TAX $0.03
TOTAL $23.80
Uisa
What regular expression will pull just $23.80 in this case? If I add positive lookahead, so that the expression is "(?!\d+\s+TOTAL\s+)\$+\d+\.?\d+\s+(?=.*\$\d+\.?\d+)", the result is "$23.00$0.03" and not "$23.80".
Please help. Thanks in advance.
Try this:
(?<=^TOTAL)\s*(\$\s*\d+\.?\d*)\s*$
Make sure you use MULTILINE match.
This will match all the spaces around the value, so you may want to strip those out to get the value
Example:
String in = "SPEEDWAY 3007906\n" +
"Wallace NC 28466 \n" +
"TRAM: 1086244 \n" +
"9/17/2017 2:12 pm \n" +
"Pump 08 \n" +
"Regular Unleaded \n" +
"8,716 # $2,639/6131 \n" +
"GAS TOTAL $23.00\n" +
"TAX $0.03 \n" +
"TOTAL $23.80\n" +
"Uisa ";
Pattern p = Pattern.compile("(?<=^TOTAL)\\s*(\\$\\s*\\d+\\.?\\d*)\\s*$", MULTILINE);
Matcher m = p.matcher(in);
if(m.find()) {
System.out.println(m.group(1));
}
This should print just the matched value
Maybe you could use a negative lookbehind to assert that what is before TOTAL is not GAS and capture your value in group 1.
(?<!GAS )TOTAL\s*(\$\d+\.\d+)
Demo output Java
I want to print just the stack trace starting from and at cab.ces.upgrade.controller.UpgradeRecommendationController.getPackageOffers(UpgradeRecommendationController.java:1234). This is how I get output.
**** Error Wed Jul 05 23:23:49 GMT 2017 1499297029577 /lll/dynamo/servlet/pipeline/RequestScopeManager/RequestScope-23596/cab/ces/upgrade/manager/business/UpgradeRecommendationsBusinessManager at cab.ces.upgrade.controller.UpgradeRecommendationController.getPackageOffers(UpgradeRecommendationController.java:1234)
So I need to remove **** Error Wed Jul 05 23:23:49 GMT 2017 1499297029577 /lll/dynamo/servlet/pipeline/RequestScopeManager/RequestScope-23596/cab/ces/upgrade/manager/business/UpgradeRecommendationsBusinessManager
My Code
if((strLine.contains("**** Error") )){
//|| (strLine.contains("(CreditCheckManager")
//String sPattern = "\\tat (.*)\\.(.*)\\(([^:]*):?([\\d]*)\\)";
Matcher m = p.matcher(strLine);
if(m.find()){
totCount++;
iCount++;
if((iCount==1) ){
System.out.println("Class name:- " + m.group(3));
System.out.println("Line Number:- " + m.group(4));
System.out.println();
System.out.println("VodafoneSystemException occurence count: " + exCount);
System.out.println();
System.out.println("VodafoneSystemException stack trace count: " + totCount);
}
System.out.println(strLine);
Use at (.)+ to find all string after at, See regex
This is for XQuery used in Java. My code is working with other XML files, but this time it is not returning the required data. The faulty code is below. What is wrong with it? Thanks.
String queryString =
"declare variable $docName as xs:string external;" + sep +
" for $TRACK in doc($docName)/playlist/tracklist/track " +
" return " +
" <track><title>{$TRACK/title/text()}</title>" +
" <location>{$TRACK/location/text()}</location></track>";
This is the target XML:
<?xml version="1.0"?>
-<playlist xmlns="http://xspf.org/ns/0/" version="1">
-<trackList>-<track><location>http://radiotool.com/242.mp3</location><title>New York</title></track>
-<track><location>http://radiotool.com/243.mp3</location> <title>Chicago Repeater</title></track>
</trackList></playlist>
It's probably because the source XML uses a namespace and your XPath doesn't. How about this:
String queryString =
"declare namespace xsp='http://xspf.org/ns/0/'; " +
"declare variable $docName as xs:string external;" + sep +
" for $TRACK in doc($docName)/xsp:playlist/xsp:trackList/xsp:track " +
" return " +
" <track><title>{$TRACK/xsp:title/text()}</title>" +
" <location>{$TRACK/xsp:location/text()}</location></track>";
I have the following html code segment:
<br>
Date: 2010-06-20, 1:37AM PDT<br>
<br>
Daddy: www.google.com
<br>
I want to extract
Date: 2010-06-20, 1:37AM PDT
and
Daddy: www.google.com
with the help of java regex.
So what regex I should use?
This should give you a nice starting point:
String text =
" <br>\n" +
" Date: 2010-06-20, 1:37AM PDT<br> \n" +
" <br> \n" +
"Daddy: www.google.com \n" +
"<br>";
String[] parts = text.split("(?:\\s*<br>\\s*)+");
for (String part : parts) {
System.out.println("[" + part + "]");
}
This prints (as seen on ideone.com):
[]
[Date: 2010-06-20, 1:37AM PDT]
[Daddy: www.google.com]
This uses String[] String.split(String regex). The regex pattern is "one or more of <br>, with preceding or trailing whitespaces.
Guava alternative
You can also use Splitter from Guava. It's actually a lot more readable, and can omitEmptyStrings().
Splitter splitter = Splitter.on("<br>").trimResults().omitEmptyStrings();
for (String part : splitter.split(text)) {
System.out.println("[" + part + "]");
}
This prints:
[Date: 2010-06-20, 1:37AM PDT]
[Daddy: www.google.com]
Related questions
String split array