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);
}
}
Related
How can I retrieve the location from following parsed data?
<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>
This details is within the description tag and description is already been parsed to an array list. How do just get the location out of it?
You can use the regex, (?<=Location: ).*?(?= ;) to find and extract the required match.
Solution using Stream API:
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String str = "<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>";
List<String> list = Pattern.compile("(?<=Location: ).*?(?= ;)")
.matcher(str)
.results()
.map(MatchResult::group)
.collect(Collectors.toList());
System.out.println(list);
}
}
Output:
[BLACKFORD,PERTH/KINROSS]
Non-Stream solution:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>";
Matcher matcher = Pattern.compile("(?<=Location: ).*?(?= ;)").matcher(str);
List<String> list = new ArrayList<>();
while (matcher.find()) {
list.add(matcher.group());
}
System.out.println(list);
}
}
Output:
[BLACKFORD,PERTH/KINROSS]
Explanation of the regex at regex101:
If all you get is
Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0
You're going to have to either (a) determine the standard that dictates this format if any or (b) do it yourself i.e. look at the structure and decide to parse based on that.
Simple way with split()
It seems you can use the split() method on a String using separator " ; ". That should give you an array of length 5.
You could then assume Location is always in the second position or simply iterate over the array until you find the string that starts with Location.
Example
public class Location {
public static void main(String[] args) {
String rawData = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0\r\n";
String[] dataArray = rawData.split(" ; ");
System.out.println(dataArray[1]);
}
}
The Regular Expression Way
Alternatively, you can use a regular expression that could give you the value outright without going through the steps I just described. The value you are looking for is always preceded by Location: and ends with ; Have a look at this primer to get going.
Pattern pattern = Pattern.compile("(?<=Location: ).*?;", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(rawData);
boolean matchFound = matcher.find();
if(matchFound) {
System.out.println("Match found: "+matcher.group());
} else {
System.out.println("Match not found");
}
Use a dictionary along with Regex :
string pattern = #"(?'key'[^:]+):\s+(?'value'.*)";
string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
string[] splitArray = input.Split(new char[] { ';' });
Dictionary<string, string> dict = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
string location = dict["Location"];
Or this
string pattern = #"(?'key'[^:]+):\s+(?'value'[^;]+);?";
string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
string[] splitArray = input.Split(new char[] { ';' });
MatchCollection matches = Regex.Matches(input, pattern);
Dictionary<string, string> dict = matches.Cast<Match>()
.GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
string location = dict["Location"];
Try
String desc = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
String[] parts = desc.split(";");
for ( String part : parts )
{
if ( part.contains("Location") )
{
parts = part.split(":");
System.out.println("***************** Location is: '" + parts[1].trim() + "'");
break;
}
}
I have a flat file with data in following format:
1:00 PM
Name UniqueID
ABX 298819 12 519440AD3
12:00 AM
Name UniqueID
AX1 239949 01 119440AD3
Where each section starts with a time, followed by headers and then values. I am trying to capture each of these sections through regex, so I can get:
section 1:
1:00 PM
Name UniqueID
ABX 298819 12 519440AD3
section 2:
12:00 AM
Name UniqueID
AX1 239949 01 119440AD3
And later parse each of these sections in to java class object, which is given below:
public class Section {
String timestamp;
List<Row> rows;
}
public class Row {
String name;
String uniqueId;
}
but I am not able to extract the "text" between two positive regex matches. Below is the regular expression i tried:
((1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm))(?=.*)
But it returns only the time values:
10:30 AM
1:00 PM
1:30 PM
10:30 AM
1:00 PM
1:30 PM
I even tried adding Pattern.MULTILINE to Pattern but it didn't work either.
Assuming the structure you showed us repeats throughout the file, then there are four types of lines in sequence: timestamp, header, data, empty line.
For example, if you want to separate the unique ID from the name, you could try:
String third = "ABX 298819 12 519440AD3";
String uniqueId = third.replaceAll(".*\\s+(\\w+)", "$1");
String name = third.replaceAll("(.*)\\s+\\w+", "$1");
I'm using this regex to match and replace a part of a string:
(.*)<a href=\\"(.*)\\" class=\\"PopupItemLink\\">(.*)<\\\/a>(.*)
This string is an example: (https://regexr.com/3n1f1)
\n&7This is the alert body\n\nYour name: HAlexTM\nYour ID: 1\nHere the link: <a href=\"test.com\" class=\"PopupItemLink\">Hey<\/a>\n\nThis is a html test: <p>Hey<\/p>\n&8Thu Jun 09 18:07:30 CEST 2016
This part of the string (matched by the RegEX) should be replaced with Hey
<a href=\"test.com\" class=\"PopupItemLink\">Hey<\/a>
So in Java I use this code
if (asit.matches("(.*)<a href=\\\\\"(.*)\\\\\" class=\\\\\"PopupItemLink\\\\\">(.*)<\\\\\\/a>(.*)")) {
asit.replaceAll("<a href=\\\\\"(.*)\\\\\" class=\\\\\"PopupItemLink\\\\\"", "$1");
asit.replaceAll(">(.*)<\\\\\\/a>", "$1");
return asit;
}
But it doesn't return anything, what's the problem?
I've resolved it removing the if block and write just replaceAll(), reassigning the variable value since Strings are immutable (thanks to #PM77-1)
output = output.replaceAll("<a href=\"(.*)\" class=\"PopupItemLink\"", "$1");
output = output.replaceAll(">(.*)<\\/a>", " ($1)");
return output;
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();
}
January 22, 2014
I want to split the string into three but the second has , and space
If I understand you, you could use a single (zero or one matches) regular expression with something like -
String in = "January 22, 2014";
String[] arr = in.split(",?\\ ");
for (String str : arr) {
System.out.println(str);
}
Output is
January
22
2014