Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式..检索文本并替换_Java_Regex - Fatal编程技术网

Java 正则表达式..检索文本并替换

Java 正则表达式..检索文本并替换,java,regex,Java,Regex,我有一个xml文件,如: <pre> <book category="CHILDREN"> <details title="Harry Potter" author="J K. Rowling"> </book> </pre> 如何对字符串(在xml文件中)使用子字符串并用xyzName替换作者 提前感谢 使用以下正则表达式替换: String contents=“\n”+ “\n”+ “\n”+ “\

我有一个xml文件,如:

<pre>
    <book category="CHILDREN">
      <details title="Harry Potter" author="J K. Rowling">
    </book>
</pre>

如何对字符串(在xml文件中)使用子字符串并用xyzName替换作者

提前感谢

使用以下正则表达式替换:

String contents=“\n”+
“\n”+
“\n”+
“\n”+
"";

String newContents=contents.replaceFirst(“(?您确定要这样做吗?如果不显示任何代码,您可能会遇到法律麻烦。:p您显示的不是格式良好的xml文档。不要为此使用正则表达式,请使用适当的DOM您不应该
substring()
-使用xml解析器执行此操作!打印re.sub(r“author=)*,r'author=“xyz”>,x)。
String contents = "<pre>\n" +
                      "<book category=\"CHILDREN\">\n" +
                        "<details title=\"Harry Potter\" author=\"J K. Rowling\">\n" +
                      "</book>\n" +
                  "</pre>";
String newContents = contents.replaceFirst("(?<=author=\")[^\"]+", "xyzName");