Regex Match Ampersand but not escaped xml characters

后端 未结 1 1492
梦如初夏
梦如初夏 2020-12-17 10:18

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

'
"
>
<
&
&#

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 10:46

    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
    

    0 讨论(0)
提交回复
热议问题