Reputation: 6039
I am really confused on this regex things. I have tried to understand it, went no where.
Basically, i am trying to replace all spaces followed by every character but a space to be replaced with "PM".
" sd" " sd"
however
" sd" " sd"
Upvotes: 0
Views: 71
Reputation: 9365
This will replace the space and the following character with "PM":
String s = "123 axy cq23 dasd"; //your string
String newString = s.replaceAll(" [^ ]","PM");
Since I'm not sure if you want to replace only the space or the space and the following character, too, here is a slightly modified version that replaces only the space:
String s = "123 axy cq23 dasd"; //your string
String newString = s.replaceAll(" ([^ ])", "PM$1")
Upvotes: 1
Reputation: 137282
You need to use non-capturing pattern:
String res = oldString.replaceAll(" (?:[^ ])", "PM");
Upvotes: 1