你可以很容易地写自己。以下是如何:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegEx {
static public interface MatchProcessor {
public String processMatch(final String $Match);
}
static public String Replace(final String $Str, final String $RegEx, final MatchProcessor $Processor) {
final Pattern aPattern = Pattern.compile($RegEx);
final Matcher aMatcher = aPattern.matcher($Str);
final StringBuffer aResult = new StringBuffer();
while(aMatcher.find()) {
final String aMatch = aMatcher.group(0);
final String aReplacement = $Processor.processMatch(aMatch);
aMatcher.appendReplacement(aResult, aReplacement);
}
final String aReplaced = aMatcher.appendTail(aResult).toString();
return aReplaced;
}
static public void main(final String ... $Args) {
final String aOriginal = "A xx1 B xx2 C xx3 D";
final String aRegEx = "xx\\d";
final String aReplaced = Replace(aOriginal, aRegEx, new MatchProcessor() {
public String processMatch(final String $Match) {
int num = Integer.parseInt($Match.substring(2));
return ""+(num*num);
}
});
System.out.println(aReplaced);
}
}
希望这有帮助。