Take a look at the String.matches()
method. If you construct the regular expression properly it should be able to do what you want it to. A good place to start for regular expressions would be the Java tutorials: http://docs.oracle.com/javase/tutorial/essential/regex/