Vidya
Vidya

Reputation: 223

How to process a line and get key words from them

I have the line read from a file as follows:

ABC: Def, XYZ-ID: Xbase::Something, here, 90, EFG: something: again

The key words are ABC, XYZ-ID and EFG and the values of these key words follow.

How do I process the line such that I can further use the code below to process the key and value accordingly:

String[] keyValArray = str.split(":");
String key = keyValArray[0];
String val = keyValArray[1];

P.S: The key words are all in capitals.

Thanks in advance.

Upvotes: 1

Views: 43

Answers (1)

Moishe Lipsker
Moishe Lipsker

Reputation: 3032

If you are okay using regular expressions and are expecting three variables, you could use something like this:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
String str = "....";
Pattern varPattern = Pattern.compile("^.*?:(.*),.*?:(.*),.*?:(.*)$");
Matcher varMatcher = varPattern.matcher(str);
while(varMatcher.find()) {
    //do something with varMatcher.group(1)
}

Upvotes: 2

Related Questions