Regex Match Ampersand but not escaped xml characters

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:33:05

问题


I would like to match ampersand (&) but not when it exists in following manner

'
"
>
<
&
&#

So in the following line & MY& NAME IS M&Hh. ' " > < & &# &&&&&&

I want it to match all ampersands except those which exist in ' " > < & &#


回答1:


That looks like a job for negative lookahead assertions:

&(?!(?:apos|quot|[gl]t|amp);|#)

should work.

Explanation:

&        # Match &
(?!      # only if it's not followed by
 (?:     # either
  apos   # apos
 |quot   # or quot
 |[gl]t  # or gt/lt
 |amp    # or amp
 );      # and a semicolon
|        # or
 \#      # a hash
)        # End of lookahead assertion


来源:https://stackoverflow.com/questions/16423089/regex-match-ampersand-but-not-escaped-xml-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!