I have a string in that match the substring and append the substring with #tag like #Test#;
String str = 'This is a TesT for reteSt the tEsTing which will confirm that test was not TEsTed perfectly by teSTer';
String str2 = 'test';
return 'This is a #TesT# for re#teSt# the #tEsT#ing which will confirm that #test# was not #TEsT#ed perfectly by #teST#er';
i have tried in many ways iam unable to achieve output;
please help
String myString1 = 'This is a TesT for reteSt the tEsTing which will confirm that test was not TEsTed perfectly by teSTer';
String myString2 = 'test';
string replacement='#test#';
string result = myString1.replaceAll('(?i)'+myString2,replacement);
system.debug(result);
iam getting result but it is case insensitive.
thanks for any help.
Best Answer
This is much easier and less code to implement by means of regex.
You did a good attempt. A couple of minor changes are required.
Take a look at the working online example https://regex101.com/r/FBhTZp/1 it also contains an explanation to it.
(?i)(test)
- any part of the expression to the right of(?i)
is a case insensitive.#$0#
-$0
matches the found group. In this regex, we have only one group.result is: