Delete every file except some

There are different solution to this task. The most common are:

rm * | grep -v [pattern to ignore]
for i in `ls * | grep -v [pattern to ignore]`
do
   do something here
done
find . ! -name u ! -name p -maxdepth 1 -type f -delete

and many others, but the most clean and elegant solution is

rm !(file1 | file2)

NB: If you try this latter solution in a bash script you'll find out it's not working unless you enable the extended pattern matching.

#extended pattern matching on
shopt -s extglob

rm -f !(file1 | file2)
#or recursive
rm -rf !(file1 | file2)

#extended pattern matching off
shopt -u extglob