繁体   English   中英

Java正则表达式删除xml结束标记中空格后的所有字符

[英]Java regex to remove all the characters after space in the end tag of an xml

我有以下格式的XML文件。

<?xml version="1.0" encoding="UTF-8"?>
<SampleData ID="Test" Password="Test">
<STATUS operation=”remove”>EXPIRED</STATUS operation=”remove”>
<PRIVILEGE operation=”remove”>12345</PRIVILEGE operation=”remove”>
<userID>ABC123</userID>
<PROFILE operation=”remove”>DEFAULT</PROFILE operation=”remove”>
</SampleData>

在此XML中,我不需要空格后的end标记中的任何文本。 例如,如果您考虑结束标记</STATUS operation=”remove”> ,我只希望它显示为</STATUS> 如果任何结束标签中没有空格,则该标签可以保持不变。 另外,开始标记在任何情况下都将保持不变。

有人可以给我建议任何正则表达式,让我可以解析整个XML并检查每个结束标签,以便我可以删除这些标签中空格后的所有字符。

这是完成此操作的一种方法:

final String regex = "(<\\/.*)\\ (.*)>";

final String string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
         + "<SampleData ID=\"Test\" Password=\"Test\">\n"
         + "<STATUS operation=”remove”>EXPIRED</STATUS operation=”remove”>\n"
         + "<PRIVILEGE operation=”remove”>12345</PRIVILEGE operation=”remove”>\n"
         + "<userID>ABC123</userID>\n"
         + "<PROFILE operation=”remove”>DEFAULT</PROFILE operation=”remove”>\n"
         + "</SampleData>";

final String subst = "$1>";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println(result);

输出

<?xml version="1.0" encoding="UTF-8"?>
<SampleData ID="Test" Password="Test">
<STATUS operation=”remove”>EXPIRED</STATUS>
<PRIVILEGE operation=”remove”>12345</PRIVILEGE>
<userID>ABC123</userID>
<PROFILE operation=”remove”>DEFAULT</PROFILE>
</SampleData>

在这里测试: Regex 101

暂无
暂无

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

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