Hello, everybody!
I need to come up with a regex to replace all ocurrences of an empty string except the last one. Right now I'm doing this with this code:
public static void main(String[] args)
{
String str = "abc";
String regex = "";
System.out.println(str.replaceAll(regex, "-").replaceFirst("-$", "")); // -a-b-c
}
As you can see, to do this I have to make two calls: one for replaceAll and another for replaceFirst to get rid of the last character. So I would like to get rid of the call to replaceFirst and have the
regex variable to be an regex that would insert the character along all the string except the last one. I'm really not very good with regexs, so I'm asking your help here. The thing that I have is working, but I think it would be more elegant with just one regex to do it.
Thank you in advance.
Marcos