You should use replaceAll. This method uses two arguments
regex for substrings we want to find
replacement for what should be used to replace matched substring.
In replacement part you can use groups matched by regex via $x where x is group index. For example
"ab cdef".replaceAll("[a-z]([a-z])","-$1")
will produce new string with replaced every two lower case letters with - and second currently matched letter (notice that second letter is placed parenthesis so it means that it is in group 1 so I can use it in replacement part with $1) so result will be -b -d-f.
Now try to use this to solve your problem.