First of all, there is a nice overview of regular expressions in
Java API for
Pattern class (
here). I even use it for reference, when working with regexps in other languages.
Also you may want to have a look at pretty good tutorial on regular expressions from Sun
here.
So, before asking such questions, you could try to figure it out,by first, learning the basics about regular expressions.
Anyway, the correct pattern in your case would be:
^foo$
As you can find in documentation for Pattern class, ^ stands for the beginning of a line, and $ for the end.