!(pattern) is ksh glob syntax, in zsh, you use ^(pattern) to negate the matching when extendedglob enabled:
setopt extendedglob
print -rl -- ^(2test|3test)
If you want to use ksh syntax, you need to enable kshglob:
setopt kshglob
print -rl -- !(2test|3test)
You can also use the and-not/except operator:
setopt extendedglob
print -rl -- *test~[23]*
(*test files except those that start with 2 or 3).
Also not that unless the nobareglobqual option is enabled or you use |s within them, trailing (...) glob grouping operators conflict with glob qualifiers. For example, in !(foo) or ^(foo), the foo would be treated as a glob qualifier. You'd need ^foo or !(foo)(#q) (the (#q) adds a non-bare (explicit) glob qualifier).
^(2test|3test),!works for bash, zsh however uses^. – Marco Dec 01 '15 at 08:17rm ^(2test|3test), still doesn't work – Aaron Shen Dec 01 '15 at 08:18touch {1..10}test; rm ^(2test|3test)leaves only the two files. If it doesn't work for you please provide your zsh version. Also make sure you usesetopt extendedglobwhich is required for this to work. – Marco Dec 01 '15 at 08:20setopt kshglobto enable!(...). – Michael Homer Dec 01 '15 at 08:25extendedglob. – Marco Dec 01 '15 at 08:25extendedglobworks forrm ^(2test|3test), but still doesn't work for 'rm !(2test|3test), what's the difference between^and!, why some online post says to use!`?? – Aaron Shen Dec 01 '15 at 08:27