Working example:
public class Main {
public static void main(String[] args) {
for (String prefix : new String[] { "pre", "" }) {
for (String suffix : new String[] { "suf", "" }) {
String toReplace = "lala";
String before = prefix + toReplace + suffix;
System.out.println(before + " -> "
+ before.replaceAll("(?<=.)" + toReplace
+ "|" + toReplace + "(?=.)", "[replaced]"));
}
}
}
}
Output:
prelalasuf -> pre[replaced]suf
prelala -> pre[replaced]
lalasuf -> [replaced]suf
lala -> lala
Information on the relevant regex syntax.
As was commented, it's strange that you've written //w instead of \\w for a word character. I'm not sure how that got replaced before.
Also, you said you wanted to match any char which means you want to use ., not \\w.