Java 如何用与模式匹配的新值替换字符串?

Java 如何用与模式匹配的新值替换字符串?,java,regex,xml,Java,Regex,Xml,应用程序将接收类似以下内容的XML消息(输入): String input = "<?xml version="1.0" encoding="utf-8"?> <message> <property name="name1" value="value1" type="A"/> <property name="name2" value="valu%@1" type="B"/> <property name="name3" va

应用程序将接收类似以下内容的XML消息(输入):

String input = "<?xml version="1.0" encoding="utf-8"?>
<message>
   <property name="name1" value="value1" type="A"/>
   <property name="name2" value="valu%@1" type="B"/>
   <property name="name3" value="my value=\"test\"" type="B"/>
</message>";
我只希望每个值都被单独地清理和替换,而不仅仅是它的任何事件。要么它不起作用,要么我在使用replaceAll或replaceFirst时得到了非法的组引用

最有效的方法是什么


谢谢-

您在哪里使用
replaceAll()
replaceFirst()
?@jonathan-在newValue之后的while循环中。我将其更改为以下内容,它可以工作:while(matcher.find()){String oldName=matcher.group();String newName=StringEscapeUtils.escapeXml(oldName);input=input.replace(oldName,newName);}问题现在解决了吗?
String regex = "(?<=value=\")([^\"]+).*?(?=\" type=\")";
Pattern pattern = Pattern.compile (regex);
Matcher m = pattern.matcher (input);
while (matcher.find()) {
  String oldValue = m.group(1);
  String newValue = StringEscapeUtils.escapeXml(oldValue);
  ??
}