如何从课程中排除某些字符?

pla*_*etp 32 regex perl character-class

假设我想匹配"单词"字符(\w),但排除"_",或匹配空白字符(\s),但排除"\ t".我怎样才能做到这一点?

yst*_*sth 46

使用包含\ W或\ 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
Run Code Online (Sandbox Code Playgroud)

  • +1我称这种技术为[双阴](http://stackoverflow.com/questions/3469080/match-whitespace-but-not-newlines-perl/3469155#3469155). (7认同)