This is my first post. I have problem with String in Java. I want to use regular expression. Thank you in advance for any help.
My String is:
String str = "abXYxyzXYZabXasdfggXYZ".
I want to replace all characters with "+" but not variable below:
String word = "XYZ"
So the output should be like that:
+++++++XYZ+++++++++XYZ.
I started out with:
str.replaceAll("[^" + word + "]", "+")
But I got different output like that:
++XY+++XYZ++X++++++XYZ
java.util.regexdoes not support a construct that matches any text other than a certain multicharacter string of text. Almost all regex flavors do not support it.[...]in regex denotes a set of single characters. You can't that easily denote the inverse of whole words. So[^XYZ]matches every character that is notX, notYand notZ, so the whole alphabet exceptX, Y, Z. It does not match whole words that are unequal to the wordXYZ.