Code: Select all
String str = "she sells sea shells";
String rex = "\\Ss\\S";
String replace = "X";
Pattern p = Pattern.compile(rex);
Matcher m = p.matcher(str);
String val = m.replaceAll(replace);
System.out.println(val);
Explanation: \\S implies any non-white space character. Therefore, \\Ss\\S matches any s that is surrounded by a non white space character on both sides. There is no such s in the given input and so there is no change.
Ok, explanation perfectly clear, tested that piece of code, got the output as expected she sells sea shells. But isn't there two s so rounded by NON white space? she sells sea shells. They both so rounded by beginning&end of string and characters. I mean there is no white space at beginning and end of that string???
Could anyone please clear this?
Thanks in advance.