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
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 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));
I am trying to generate a message in Java for an email. The generated email works fine in Dev and test environment, But UAT is not detecting some of EOL, In same email few occurrences are working and Some are not working. Please find below the code to get the Email Message:
String genMessage(Suport me) {
public String EOL = "\r\n";
StringBuffer sb = new StringBuffer();
sb.append(EOL);
sb.append(EOL);
sb.append("The item below is now travelling");
sb.append(EOL);
sb.append("Reason for the delay: ");
sb.append(me.getReason());
sb.append(EOL);
sb.append("item Number: " + meBO.getId());
sb.append(EOL);
ReferenceBO deliveryNumber = me.getDNumber();
sb.append("Del Number: " + (dNumber != null ? deliveryNumber.getValue() : ""));
sb.append(EOL);
sb.append(EOL);
sb.append("Carrier: " + me.getCarrier() + " - " + me.getCarrierName());
sb.append(EOL);
sb.append(EOL);
sb.append("Original Delivery Date/Time: E: " + getDate(me.getEarliestDelDatetime())));
sb.append(EOL);
sb.append("Original Delivery Date/Time: L: " + getDate(me.getLatestDelDatetime())));
sb.append(EOL);
return sb.toString();
}
The Email generated :
The item below is now travelling
Reason for the delay: Vehicle breakdown or accident item Number: 644812 Del Number: TEST20170925
Carrier: ABC - ABC EXPRESS, PVT LTD.
Original Delivery Date/Time: E: September 27, 2017 8:00 AM Original Delivery Date/Time: L: September 27, 2017 5:00 PM
So, here after 1st line and also before and after Carrier: EOL looks to be working but not at other places.
Any Suggestion will be Helpful.
Thanks for your solution #maxBilbow.
The issue was due to Outlook changing the format and ignoring some of the line changes and spaces it assumed unnecessary.
Adding 3 or more spaces at the start of each line fixed the issue.
replaced public String EOL = "\r\n"; with public String EOL = "\r\n ";
I am trying to solve this question:
Get document on some condition in elastic search java API
My logic is first we get all the position of months which is in string, After that i extract next word which is a 4 digit or 2 digit year, Then calculate difference using this.
For getting months position i am using this piece of code:-
String[] threeMonthArray=new String[]{" Jan "," Feb "," Mar "," Apr "," May "," June "," July "," Aug "," Sep "," Oct "," Nov "," Dec "};
String[] completeMonthArray=new String[]{"January","Feburary","March","April","May","June","July","Augest","September","October","November","December"};
List indexArray=new ArrayList();
for(int i=0;i<threeMonthArray.length;i++){
int index = parsedContent.toLowerCase().indexOf(threeMonthArray[i].toLowerCase());
while (index >= 0) {
System.out.println(threeMonthArray[i]+" : "+index+"------");
indexArray.add(index);
index = parsedContent.toLowerCase().indexOf(threeMonthArray[i].toLowerCase(), index + 1);
}
// System.out.println(threeMonthArray[i]+" : "+parsedContent.toLowerCase().indexOf(threeMonthArray[i].toLowerCase())+"------");
}
Collections.sort(indexArray);
System.out.println( indexArray);
And it's showing this output:-
[2873, 2884, 3086, 3098, 4303, 4315, 6251, 6262, 8130, 8142, 15700, 15711]
I am getting correct position. My problem is how i can get next word which must be a digit.
Jun 2010 to Sep 2011 First Document
Jun 2009 to Aug 2011 Second Document
Nov 2011 – Sep 2012 Third Document
Nov 2012- Sep 2013 Forth Document
You can use a regular expression to find the next number starting at the position of your last found month:
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(parsedContent);
if (m.find(index)) {
String year = m.group();
}
I have a String as "AASS MON 01 2013 365.00 HJJ Call"
I need to remove the String HJJ from the above String and need the output as
AASS MON 01 2013 365.00 HJJ Call
I tried the following thing
if(symbol.contains("HJJ"))
{
symbol = symbol.replace("HJJ","");
}
But with this i am getting output as
AASS MON 01 2013 365.00 Call ( One extra space before call )
Where i want it to be
AASS MON 01 2013 365.00 Call
Here is what I usually use:
public static String removeExtraSpaces(String input) {
return input.trim().replaceAll(" +", " ");
}
trim removes beginning and ending spaces while replaceAll replaces any line of spaces by one single space.
public class Trimimg
{
public static void main(String[]args)
{
String str = "AASS MON 01 2013 365.00 HJJ Call";
String newStr = str.replace(" HJJ", "");
System.out.println(newStr);
}
}