Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Match Ampersand but not escaped xml characters

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 ' " > < & &#

like image 916
Munish Avatar asked May 07 '13 15:05

Munish


1 Answers

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
like image 100
Tim Pietzcker Avatar answered Oct 06 '22 06:10

Tim Pietzcker