2010-08-23 11 views
22

Dire che voglio abbinare un carattere "word" (\w), ma escludere "_", o abbinare un carattere di spazio (\s), ma escludere "\ t". Come posso fare questo?Come posso escludere alcuni caratteri da una classe?

+3

A beneficio di altri che potrebbero utilizzare Java, .NET, schema XML, o JGSoft/RegexBuddy, c'è in realtà un meccanismo di carattere sottrazione di quei sapori: http://stackoverflow.com/questions/3201689/character-class-sottraction-converting-from-java-syntax-to-regexbuddy; per esempio. '[a-z- [aeiou]]' in .NET corrisponde a una consonante minuscola. – polygenelubricants

risposta

36

Utilizzare una classe negata che include \ W o \ S.

/[^\W_]/ # anything that's not a non-word character and not _ 
/[^\S\t]/ # anything that's not a non-space character and not \t 
+4

+1 Io chiamo questa tecnica a [doppio-negativo] (http://stackoverflow.com/questions/3469080/match-whitespace-but-not-newlines-perl/3469155#3469155). –

Problemi correlati