I'm attempting to match on a substring and key value pair. For example, matching on the string :
"\"a,b,c\",,\"$a = test1, $1 = test2, $2 = test2\",3\n"
a
should return
a test1
1 test2
2 test2
a
b
c
where
a test1
2 test2
is a Map and a,b,c
are List
of items.
Below code :
import javafx.util.Pair;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class KeyValuesExtract {
private Pair<List<String>, Map<String, String>> getKeyValues(final String line) {
final Pattern quotesPattern = Pattern.compile("\"(.*?)\"");
final Matcher quotesMatcher = quotesPattern.matcher(line);
quotesMatcher.find();
final List<String> vids = Arrays.asList(quotesMatcher.group(0).split(",")).stream().map(x ->
x.replace("\"", "").trim()).collect(Collectors.toList());
final Map<String, String> enumKeyValuePairs = new HashMap<>();
final Pattern keyValuePattern = Pattern.compile("\"\\$([A-Za-z0-9]+)\\s=\\s(\\w+)(?:,\\s\\$([A-Za-z0-9]+)\\s=\\s(\\w+))*\"");
final Matcher keyValueMatcher = keyValuePattern.matcher(line);
while (keyValueMatcher.find()) {
for (int i = 1; i <= keyValueMatcher.groupCount(); i++) {
enumKeyValuePairs.put(keyValueMatcher.group(i), keyValueMatcher.group(++i));
}
}
return new Pair(vids, enumKeyValuePairs);
}
public static void main(String args[]) {
final String str = "\"a,b,c\",,\"$a = test1, $1 = test2, $2 = test2\",3\n";
final KeyValuesExtract testCode = new KeyValuesExtract();
final Pair<List<String>, Map<String, String>> pair = testCode.getKeyValues(str);
pair.getValue().entrySet().forEach(entry -> {
System.out.println(entry.getKey() + " " + entry.getValue());
});
pair.getKey().forEach(entry -> {
System.out.println(entry);
});
}
}
prints :
a test1
2 test2
a
b
c
From an earlier question : Extracting key value pair from substring within string I've updated the regex from
"\"\\$(\\d+)\\s=\\s(\\w+)(?:,\\s\\$(\\d+)\\s=\\s(\\w+))*\""
to match on both digits and characters :
"\"\\$([A-Za-z0-9]+)\\s=\\s(\\w+)(?:,\\s\\$([A-Za-z0-9]+)\\s=\\s(\\w+))*\""
How to match all the Map values ? :
a test1
1 test2
2 test2