Sample Input:
a:b
a.in:b
asds.sdsd:b
a:b___a.sds:bc___ab:bd
Sample Output:
a:replaced
a.in:replaced
asds.sdsd:replaced
a:replaced___a.sds:replaced___ab:replaced
String which comes after : should be replaced with custom function.
I have done the same without Regex. I feel it can be replaced with regex as we are trying to extract string out of specific pattern.
For first three cases, it's simple enough to extract String after :, but I couldn't find a way to deal with third case, unless I split the string ___ and apply the approach for first type of pattern and again concatenate them.
2 Answers 2
Just replace only the letters with exists next to : with the string replaced.
string.replaceAll("(?<=:)[A-Za-z]+", "replaced");
or
If you also want to deal with digits, then add \d inside the char class.
string.replaceAll("(?<=:)[A-Za-z\\d]+", "replaced");
5 Comments
:bc___ can be something like this :b566 or :4545. I guess we should consider numbers too\\d inside the char class.:[email protected] or :b@_g+.com .But guranteed not to contain : and ____ as these are self-defined patternmatcher.group()while function.(:)[a-zA-Z]+
You can simply do this with string.replaceAll.Replace by 1ドルreplaced.See demo.