import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class MainClass { public static void main(String args[]) { Pattern p = null; try { p = Pattern.compile("Java \\d"); } catch (PatternSyntaxException pex) { pex.printStackTrace(); System.exit(0); } String candidate = "Java 4"; Matcher m = p.matcher(candidate); if (m != null) System.out.println(m.find()); } }