Parse the string - java

Can any one suggest how to parse the the below string?
Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}
I want Active10000000044 part out to use further next step..

If you want the right of the ":" then you can use
String str = "Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}:";
System.out.println(str.split(":")[1]);
The left can be found using
System.out.println(str.split(":")[0]);

It could be as simple as:
String str = str.replaceFirst("Added ","").replaceFirst(" .*","");
depending on whether you've given us the full suite of test data :-)
If you want the second word regardless of the first, you could try:
String str = str.replaceFirst("[^ ]+ +","").replaceFirst(" .*","");
Both those suggestions rely on the fact that the first word is not preceded by spaces and that the white space is actually spaces. Any deviation from that will require some slight tweaks.

Try this,
String str = "Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}:";
String[] parts = str.split(":");
String part1 = parts[0]; // value "Added Active10000000044"
String[] SetU_need = part1.split(" ");
String u_need = SetU_need[1]; // value "Active10000000044"

try this
Splitter class is from Google guava library
String text = "Added Active10000000044: {activityId=Active1, schedule=1 22 * * 0, isEnabled=Y, type=global, runAtHost=null}:";
int indexOfOpenBrace = text.indexOf("{");
int indexOfCloseBrace = text.indexOf("}");
String valuesAsText = text.substring(indexOfOpenBrace+1, indexOfCloseBrace);
List<String> splitToList = Splitter.on(",").omitEmptyStrings().splitToList(valuesAsText);
Map<String, String> map = new HashMap<>();
for (String keyValues : splitToList) {
List<String> splitToKeyAndValues = Splitter.on("=").omitEmptyStrings().splitToList(keyValues);
map.put(splitToKeyAndValues.get(0), splitToKeyAndValues.get(1));
}
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key+":"+map.get(key));
}
Output
activityId:Active1
schedule:1 22 * * 0
type:global
runAtHost:null
isEnabled:Y

Related

Extract Substring from String java

I want to extract specific substrings from a string:
String source = "info1 info1ContentA info1ContentB info3 info3ContentA info3ContentB"+
"info2 info2ContentA";
The result should be:
String info1 ="info1ContentA info1ContentB";
String info2 ="info2ContentA";
String info3 ="info3ContentA info3ContentB";
For me it's very difficult to extract the informations, because sometimes after "info" their are one, two or more content informations. Another problem that occurs is, that the order of info1, info2 etc. is not sorted and the "real data" doesn't contain a ascending number.
My first idea was to add info1, info2, info3 etc to an ArrayList.
private ArrayList<String> arr = new ArrayList<String>();
arr.add("info1");
arr.add("info2");
arr.add("info3");
Now I want to extract the substring with the method StringUtils.substringBetween() from Apache Commons (https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4):
String result = StringUtils.substringBetween(source, arr.get(0), arr.get(1));
This works, if info1 is in the string before info2, but like I said the "real data" is not sorted.
Any idea how I can fix this?
Split those string by space and then use String's method startsWith to add the part to proper result string
Map<String, String> resultMap = new HashMap<String, String>();
String[] prefixes = new String[]{"info1", "info2", "info3"};
String source = "info1 info1ContentA info1ContentB info3 info3ContentA info3ContentB"+" info2 info2ContentA";
String[] parts = source.split(" ");
for(String part : parts) {
for(String prefix : prefixes) {
if(part.startsWith(prefix) {
String currentResult = (resultMap.containsKey(prefix) ? resultMap.get(prefix) + part + " " : part);
resultMap.put(prefix, currentResult);
}
}
}
Also consider using StringBuilder instead of adding string parts
If you cannot be sure that parts will be embraces with spaces you can change at the beginning all part to <SPACE>part in your source string using String replace method
You can use a regular expression, like this:
String source = "info1 info1ContentA info1ContentB info3 info3ContentA info3ContentB info2 info2ContentA";
for (int i = 1; i < 3; i++) {
Pattern pattern = Pattern.compile("info" + i + "Content[A-Z]");
Matcher matcher = pattern.matcher(source);
List<String> matches = new ArrayList<>();
while (matcher.find()) {
matches.add(matcher.group());
}
// process the matches list
}

Parsing String in Java, then storing in variables

I need help to parse a string in Java... I'm very new to Java and am not sure how to go about it.
Suppose the string I want to parse is...
String str = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;"
What I would want to do is:
String name = C43
String direction = EB2;
Then what I'd like to do is store 2 coordinates as a pair...
Coordinate c1 = 49.21716;-122.667252;
Coordinate c2 = 49.216757;-122.666235;
And then make a List to store c1 and c2.
So far I have this:
parseOnePattern(String str) {
String toParse = str;
name = toParse.substring(1, toParse.indexOf("-"));
direction = toParse.substring(toParse.indexOf("-", toParse.indexOf(";")));
I'm not sure how to move forward. Any help will be appreciated.
A simple substring function may solve your problem.
String str = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;";
String[]s = str.split(";");
String[]n = s[0].split("-");
String name = n[0].substring(1);
String direction = n[1];
String c1 = s[1] +";"+s[2];
String c2 = s[3] +";"+s[4];
System.out.println(name + " " + direction);
System.out.println(c1 + " " + c2);
I hope this helps you.
Welcome to Java and the whole set of operations it allows to perform on Strings. You have a whole set of operations to perform, I will give you the code to perform some of them and get you started :-
public void breakString() {
String str = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235";
// Will break str to "NC43-EB2" and "49.21716" "-122.667252" "49.216757" "-122.666235"
String [] allValues = str.split(";", -1);
String [] nameValuePair = allValues[0].split("-");
// substring selects only the specified portion of string
String name = nameValuePair[0].substring(1, 4);
// Since "49.21716" is of type String, we may need it to parse it to data type double if we want to do operations like numeric operations
double c1 = 0d;
try {
c1 = Double.parseDouble(allValues[1]);
} catch (NumberFormatException e) {
// TODO: Take corrective measures or simply log the error
}
What I would suggest you is to go through the documentation of String class, learn more about operations like String splitting and converting one data type to another and use an IDE like Eclipse which has very helpful features. Also I haven't tested the code above, so use it as a reference and not as a template.
Ok i made this:
public static void main(String[] args) {
String str = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;";
String[] strSplit = str.split(";");
String[] nameSplit=strSplit[0].split("-");
String name=nameSplit[0].replace("N", "");
String direction= nameSplit[1];
String cordanateOne = strSplit[1]+";"+strSplit[2]+";";
String cordanateTwo = strSplit[3]+";"+strSplit[4]+";";
System.out.println("Name: "+name);
System.out.println("Direction: "+direction);
System.out.println("Cordenate One: "+cordanateOne);
System.out.println("Cordenate Two: "+cordanateTwo);
}
Name: C43
Direction: EB2
Cordenate One: 49.21716;-122.667252;
Cordenate Two: 49.216757;-122.666235;
String str3 = "NC43-EB2;49.21716;-122.667252;49.216757;-122.666235;";
String sub = str3.substring(0,4); // sub = NC43
String sub4 = str3.substring(5,9); // sub = EB2;
HashMap<String, String> hm = new HashMap<>();
hm.put(str3.substring(9 ,30), str3.substring(30));
hm.forEach((lat, lot) -> {
System.out.println(lat + " - " + lot); // 49.21716;-122.667252; - 49.216757;-122.666235;
});
//edit if using an array non pairs (I assumed it was lat + lon)
List<String> coordList = new ArrayList<>();
coordList.add(str3.substring(9 ,30));
coordList.add(str3.substring(30));
coordList.forEach( coord -> {
System.out.println(coord);
});
//output : 49.21716;-122.667252;
49.216757;-122.666235;

parse and capture the numbers that are at the end of strings

I am building a program to go through a log file that has entries like this:
en halo%20reach%20noble%20actual%20in%20theater 1 659
en Wazir_Khan_Mosque 2 77859
en Waziristan_War 3 285976
en Wazirpur_Upazila 1 364
I want to output the numbers that appear at the end of each string (ie 659, 77859, 285976, 285976, 364). As you can see the numbers have differing amounts of digits.
How can I grab the last numbers from these strings?
One possible solution is to split the String according to whitespaces:
String[] splitted = myStr.split("\\s+");
Then take the last element:
splitted[splitted.length - 1];
If you want to int value, you should use Integer#parseInt.
Another solution is using lastIndexOf and substring..
int pos = line.lastIndexOf(' ');
int value = Integer.parseInt(line.substr(pos+1));
If you are reading each line and assigning to a string like this
String line = "en halo%20reach%20noble%20actual%20in%20theater 1 659";
then doing this should give you the last number
String words[] = line.split("\\s");
System.out.println(words[words.length - 1]);
I usually don't recommend regular expressions as they are so often abused here on Stackoverflow ( especially when it comes to XML/HTML ), but this the perfect case to learn how to use them!
Splitting on whitespace, while that will work isn't as robust as this approach; which will continue to work if the whitespace varies, and allows you to capture all the other data in one operation, which you will probably want eventually:
^en\s+(.*)\s+(\d+)\s+(\d+)$ : Click for an explanation of how it works!
Then to use it:
final Pattern p = Pattern.compile("^en\\s+(.*)\\s+(\\d+)\\s+(\\d+)$");
final Matcher m = p.matches("en Wazirpur_Upazila 1 364");
final String g1 = m.group(1); // Wazirpur_Upazila
final String g2 = m.group(2); // 1
final String g3 = m.group(3); // 364
try this, may not be very good
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("log.txt"));
try {
String line = br.readLine();
List<String> stringList = new ArrayList<>();
while(line!=null) {
String[] strsplit = line.split(" ");
line = br.readLine();
for(int i=3;i<strsplit.length;i+=4) {
stringList.add(strsplit[i]);
}
}
System.out.println(stringList);
} finally {
br.close();
}

Java string matching expression String Array

I am using Java, i need your opinion on how to write better code for the following task.
I have following String value
String testStr = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(ABC) INCLUDES(ABC)"
I want to manipulate Strings and want to combine all INCLUDES statements into one INCLUDES and the result should be similar to the following:
INCLUDES(ABC,ABC, ABC) EXCLUDES(ABC, ABC)
i would break the initial string into new strings using this class:
http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
and put them in an array
start a new string, and then use the tokenizer to break out the part within the parentheses (you can set it to use ( and ) as delimiters) and loop through the array and concatenate them into the new string.
be aware though that any wrongly placed spaces (like INCLUDES( abc )) will mess it up
This seems like a reasonable approach:
Split the testStr using the StringUtils.split method; use " " or null as the token.
Create a Map<String, List<String>>. I will refer to that as theMap
For each string in the returned array perform the following:
Split the string using "()" as the token.
The returned array should have 2 elements. The first element (index 0) is key for theMap and the second element (index 1) is the value to add to the list.
Once you are done with the array returned from splitting testStr, build a new string by using the key value in theMap and appending the elements from the associated list into a string.
Apache StringUtils
I wrote a piece of code for this issue but i don't know if it's good or not
according to your format ,you can split testStr by using " " ,the output will be like this: INCLUDES(ABC)
check if this string contain INCLUDES or EXCLUDES
then split it by using ( )
Like this :
String testStr = "INCLUDES(ABC) EXCLUDES(C) EXCLUDES(ABC) INCLUDES(AC) INCLUDES(AB)";
String s[] = testStr.split(" ");
String INCLUDES = "INCLUDES( ";
String EXCLUDES = "EXCLUDES ( ";
for (int i = 0; i < s.length; i++) {
if (s[i].contains("INCLUDES")) {
INCLUDES += (s[i].substring(s[i].indexOf("(") + 1, s[i].indexOf(")"))) + " ";
}
else if (s[i].contains("EXCLUDES")) {
EXCLUDES += (s[i].substring(s[i].indexOf("(") + 1, s[i].indexOf(")"))) + " ";
}
}
INCLUDES = INCLUDES + ")";
EXCLUDES = EXCLUDES + ")";
System.out.println(INCLUDES);
System.out.println(EXCLUDES);
I have wrote down small utility result as following
if text = "EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG) INCLUDES(EFG) INCLUDES(IJK)";
output = EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG & EFG & IJK)
Following is my java codeas following please take a look and if any one can improve it please feel free.
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sun.xml.internal.ws.util.StringUtils;
/**
* Created by IntelliJ IDEA.
* User: fkhan
* Date: Aug 31, 2012
* Time: 1:36:45 PM
* To change this template use File | Settings | File Templates.
*/
public class TestClass {
public static void main(String args[]) throws Exception {
//String text = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(EFG) INCLUDES(IJK)";
String text = "EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG) INCLUDES(EFG) INCLUDES(IJK)";
List<String> matchedList = findMatchPhrase("INCLUDES", text);
String query = combinePhrase(text, "INCLUDES", matchedList);
System.out.println(query);
}
/**
* This method takes query combine and & multiple phrases
* #param expression
* #param keyword
* #param matchedItemList
* #return
*/
private static String combinePhrase(String expression, String keyword, List<String> matchedItemList) {
//if only one phrase found return value
if(matchedItemList.isEmpty() || matchedItemList.size() ==1){
return expression;
}
//do not remove first match
String matchedItem = null;
for (int index = 1; index < matchedItemList.size(); index++) {
matchedItem = matchedItemList.get(index);
//remove match items from string other then first match
expression = expression.replace(matchedItem, "");
}
StringBuffer textBuffer = new StringBuffer(expression);
//combine other matched strings in first matched item
StringBuffer combineStrBuf = new StringBuffer();
if (matchedItemList.size() > 1) {
for (int index = 1; index < matchedItemList.size(); index++) {
String str = matchedItemList.get(index);
combineStrBuf.append((parseValue(keyword, str)));
combineStrBuf.append(" & ");
}
combineStrBuf.delete(combineStrBuf.lastIndexOf(" & "), combineStrBuf.length());
}
// Inject created phrase into first phrase
//append in existing phrase
return injectInPhrase(keyword, textBuffer, combineStrBuf.toString());
}
/**
*
* #param keyword
* #param textBuffer
* #param injectStr
*/
private static String injectInPhrase(String keyword, StringBuffer textBuffer, String injectStr) {
Matcher matcher = getMatcher(textBuffer.toString());
while (matcher.find()) {
String subStr = matcher.group();
if (subStr.startsWith(keyword)) {
textBuffer.insert(matcher.end()-1, " & ".concat(injectStr));
break;
}
}
return textBuffer.toString();
}
/**
* #param expression
* #param keyword
* #return
*/
private static String parseValue(String keyword, String expression) {
String parsStr = "";
if (expression.indexOf(keyword) > -1) {
parsStr = expression.replace(keyword, "").replace("(", "").replace(")", "");
}
return parsStr;
}
/**
* This method creates matcher object
* and return for further processing
* #param expression
* #return
*/
private static Matcher getMatcher(String expression){
String patternString = "(\\w+)\\((.*?)\\)";
Pattern pattern = Pattern.compile(patternString);
return pattern.matcher(expression);
}
/**
* This method find find matched items by keyword
* and return as list
* #param keyword
* #param expression
* #return
*/
private static List<String> findMatchPhrase(String keyword, String expression) {
List<String> matchList = new ArrayList<String>(3);
keyword = StringUtils.capitalize(keyword);
Matcher matcher = getMatcher(expression);
while (matcher.find()) {
String subStr = matcher.group();
if (subStr.startsWith(keyword)) {
matchList.add(subStr);
}
}
return matchList;
}
}

Cut ':' && " " from a String with a tokenizer

right now I am a little bit confused. I want to manipulate this string with a tokenizer:
Bob:23456:12345 Carl:09876:54321
However, I use a Tokenizer, but when I try:
String signature1 = tok.nextToken(":");
tok.nextToken(" ")
I get:
12345 Carl
However I want to have the first int and the second int into a var.
Any ideas?
You have two different patterns, maybe you should handle both separated.
Fist you should split the space separated values. Only use the string split(" "). That will return a String[].
Then for each String use tokenizer.
I believe will works.
Code:
String input = "Bob:23456:12345 Carl:09876:54321";
String[] words = input.split(" ")
for (String word : words) {
String[] token = each.split(":");
String name = token[0];
int value0 = Integer.parseInt(token[1]);
int value1 = Integer.parseInt(token[2]);
}
Following code should do:
String input = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer st = new StringTokenizer(input, ": ");
while(st.hasMoreTokens())
{
String name = st.nextToken();
String val1 = st.nextToken();
String val2 = st.nextToken();
}
Seeing as you have multiple patterns, you cannot handle them with only one tokenizer.
You need to first split it based on whitespace, then split based on the colon.
Something like this should help:
String[] s = "Bob:23456:12345 Carl:09876:54321".split(" ");
System.out.println(Arrays.toString(s ));
String[] so = s[0].split(":", 2);
System.out.println(Arrays.toString(so));
And you'd get this:
[Bob:23456:12345, Carl:09876:54321]
[Bob, 23456:12345]
If you must use tokeniser then I tink you need to use it twice
String str = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer spaceTokenizer = new StringTokenizer(str, " ");
while (spaceTokenizer.hasMoreTokens()) {
StringTokenizer colonTokenizer = new StringTokenizer(spaceTokenizer.nextToken(), ":");
colonTokenizer.nextToken();//to igore Bob and Carl
while (colonTokenizer.hasMoreTokens()) {
System.out.println(colonTokenizer.nextToken());
}
}
outputs
23456
12345
09876
54321
Personally though I would not use tokenizer here and use Claudio's answer which splits the strings.

Categories