Just use procmail and write your regex rules and you are done.

ftp://ftp.informatik.rwth-aachen.de/pub/packages/procmail/