19

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

'
"
>
<
&
&#

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

我希望它匹配除了那些存在于 ' " > < & &#

4

1 回答 1

32

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

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

应该管用。

解释:

&        # 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
于 2013-05-07T15:38:51.317 回答