import java.util.regex.*; Pattern pat = Pattern.compile("a+b"); Matcher mat = pat.matcher("aaab"); boolean res = mat.matches();