Pattern.compile compiles the pattern so that when you execute metcher.matches, pattern is not re compiled again and again. Pattern.compile pre compiles it. However, if you use string.matches, it compiles the pattern everytime you execute this line. So, it is better to use Pattern.compile.