I have written a regular expression to validate a name. The name can start with alphabetics and can be followed by alphabetics, numbers, a space or a _. The regex that I wrote is:
private static final String REGEX = "([a-zA-Z][a-zA-Z0-9 _]*)*"; If the input is: "kasklfhklasdhklghjsdkgsjkdbgjsbdjKg;" the program gets stuck on matcher.matches().
Pattern pattern = Pattern.compile(REGEX); Matcher matcher = pattern.matcher(input); if (matcher.matches()) { System.out.println("Pattern Matches"); } else { System.out.println("Match Declined"); } How can I optimize the regex?