正则表达式查找特殊字符 &xml 标签之间 [英] regular expression to find special character & between xml tags

查看:27
本文介绍了正则表达式查找特殊字符 &xml 标签之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从另一个模型生成的 xml 字符串将传递给我,它可能包含一些特殊字符,例如 &在 xml 标签的文本中.
例如

A xml string generated from another model will pass to me, it may contains some special character such as & in the text of the xml tag.
e.g.

<entry>
 <key>state</key>
 <value xsi:type="xs:string">ADDDRESS  3 & ADDR 4, 12345, HONG KONG</value>
</entry>

当我从字符串构建 xml 时会有无效字符错误,所以我需要转义特殊字符 &.
我想使用正则表达式来查找 <value></value> 标记之间的 & 并替换为 &amp;
我尝试了一些,但在正则表达式上失败了.

when I build the xml from string will have invalid character error, So I need to escape the special character &.
I want to use regex to find the & between <value></value> tag and replace with the &amp;
I have tried some but fail on the regex.

谁能给我一些关于正则表达式的线索?

Can anyone give me the some clue on the regex?

此外我使用 Java 1.6

besides I use Java 1.6

推荐答案

你可以使用lookahead:

You can use lookahead:

替换

&(?!\w*;)(?=[^<]*</value>)

&amp;

这通过指定两个前瞻来工作.第一个前瞻 (?!\w*;) 阻止匹配有效的 HTML 转义序列.第二个前瞻 (?=[^<]*</value>) 指定 </value> 标签必须跟在文本之后(在一定数量的非 XML 标签内容).

This works by specifying two lookaheads. The first lookahead (?!\w*;) prevents valid HTML escape sequences from being matched. The second lookahead (?=[^<]*</value>) specifies that a </value> tag must follow the text (after some amount of non-XML-tag content).

试试这里.

这篇关于正则表达式查找特殊字符 &amp;xml 标签之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆