boolean checkValidity (String str, String regex) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); if (m.matches()) { return true; } else { return false; } }