Question: I have got a string

```
$key1={331015EA261D38A7}
$key2={9145A98BA37617DE}
$key3={EF745F23AA67243D}

```

How do i split each of the keys based on "$" and the "next line" element? 
and then place it in an Arraylist?

The output should look like this:
Arraylist[0]:

```
$key1={331015EA261D38A7}

```

Arraylist[1]:

```
$key2={9145A98BA37617DE}

```

Arraylist[2]:

```
$key3={EF745F23AA67243D}

```

Answer: 
If you just want to split by new line, it is as simple as :

```
yourstring.split("\n"):

```
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' 
> 
> 
>