2013-05-07 14 views
16

vorrei abbinare commerciale (&) ma non quando esiste nella seguente manieraRegex Partita E commerciale ma non i caratteri di escape XML

' 
" 
> 
< 
& 
&# 

Così nella riga seguente & MY& NAME IS M&Hh. ' " > < & &# &&&&&&

voglio che corrisponde a tutti e commerciali eccetto quelli esistenti in ' " > < & &#

risposta

25

Sembra un lavoro per negative lookahead assertions:

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

dovrebbe funzionare.

Spiegazione:

&  # 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 
+4

Nizza regex, Tim. Sono ancora innamorato di questo strumento, quindi scusami mentre I [link a un diagramma di fantasia] (http://www.regexper.com/#%26 (% 3F! (% 3F% 3Aapos% 7Cquot% 7C% 5Bgl % 5DT% 7Camp)% 3B% 7C% 23)). –