You must use replaceAll . This method uses two arguments
regex for the substrings we want to findreplacement for what should be used to replace substring substring.
In the replacement section, you can use the groups matched by the regular expression through $x , where x is the group index. for instance
"ab cdef".replaceAll("[az]([az])","-$1")
will create a new line with the replacement of each two lowercase letters with - and the second currently matching letter (note that the second letter is placed in brackets, so this means that it is in group 1, so I can use it in the replacement part with using $1 ), so the result would be -b -df .
Now try using this to solve your problem.
source share