I am trying to fit - between all odd numbers in a string. Therefore, if a string is passed as Hel776o , it should output Hel7-76o . Dashes should only be placed between two consecutive odd numbers.
I am trying to do this on one line through String.replaceAll ()
I have the following line:
return str.replaceAll(".*([13579])([13579]).*","$1-$2");
If there is any odd number followed by an odd number, put a - between them. But it destructively replaces everything except the last match.
For example, if I go through "999477" , it will print 7-7 instead of 9-9-947-7 . More groupings required, so I wonβt replace everything except matches?
I already did this with a traditional loop through each char line, but wanted to do it in a single line file with regular expression replacement.
Edit: I have to say what I meant by return str.replaceAll(".*([13579])([13579]).*","$0-$1"); , not $1 and $2
source share