简体   繁体   English

通过Java中的正则表达式修改xml

[英]Modifying xml via regex in java

I have the following XML String: 我有以下XML字符串:

 <asd1:content></asd1:content>

The namespace prefix asd1 could be different at different places in the XML file. 名称空间前缀asd1在XML文件中的不同位置可能不同。

I want to modify it to : 我想将其修改为:

 <asd1:content>*</asd1:content>

I am trying to do it via regex as follows: 我正在尝试通过正则表达式执行以下操作:

myString.replaceAll("<.*:content></.*:content>","replacement text");

The problem is that I don,t want to lose the namespace prefix. 问题是我不想丢失名称空间前缀。 What should I do? 我该怎么办?

Please note that you've 2 typos: 请注意,您有2种错别字:

cotent instead of content cotent ,而不是content
replaceAlll instead of replaceAll replaceAlll代替replaceAll

If you still need a regex, you can use: 如果仍然需要正则表达式,则可以使用:

String resultString = subjectString.replaceAll("(?ism)<(.*?):content></(.*?)\\.content>", "<$1:content>*</$2.content>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM