[Solved] How to split strings based on “/n” in Java [closed]


Don’t split, search your String for key value pairs:

\$(?<key>[^=]++)=\{(?<value>[^}]++)\}

For example:

final Pattern pattern = Pattern.compile("\\$(?<key>[^=]++)=\\{(?<value>[^}]++)\\}");
final String input = "$key1={331015EA261D38A7}\n$key2={9145A98BA37617DE}\n$key3={EF745F23AA67243D}";
final Matcher matcher = pattern.matcher(input);
final Map<String, String> parse = new HashMap<>();
while (matcher.find()) {
    parse.put(matcher.group("key"), matcher.group("value"));
}
//print values
parse.forEach((k, v) -> System.out.printf("Key '%s' has value '%s'%n", k, v));

Output:

Key ‘key1’ has value ‘331015EA261D38A7’
Key ‘key2’ has value ‘9145A98BA37617DE’
Key ‘key3’ has value ‘EF745F23AA67243D’

3

solved How to split strings based on “/n” in Java [closed]