正则表达式匹配&符号但不转义xml字符

Mun*_*ish 18 regex regex-negation

我想匹配&符号(&),但不是当它以下列方式存在时

'
"
>
<
&
&#
Run Code Online (Sandbox Code Playgroud)

所以在以下行中 & MY& NAME IS M&Hh. ' " > < & &# &&&&&&

我希望它匹配除了存在的所有&符号之外的所有&符号 ' " > < & &#

Tim*_*ker 28

这看起来像负面前瞻断言的工作:

&(?!(?:apos|quot|[gl]t|amp);|#)
Run Code Online (Sandbox Code Playgroud)

应该管用.

说明:

&        # 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
Run Code Online (Sandbox Code Playgroud)

  • 好的正则表达,蒂姆.我仍然喜欢这个工具,所以请原谅我[链接一个更好的图表](http://www.regexper.com/#%26(%3F!(%3F%3Aapos%7Cquot%7C%5Bgl) %5DT%7Camp)%3B%7C%23)). (4认同)