In python , I can use a replacer function in sub(), that is very powerful for some situation, like this:
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return ""
else:
return s
return re.sub(pattern, replacer, text)
how to do this in Java?
In Java, there is an idiom:
One tip that’s not readily apparent from the documentation – if yourString has special (reg exp) characters in it, it is important to use matcher.appendReplacement(stringBuffer) and matcher.appendTail(stringBuffer) to avoid errors.