I want to pattern the string like this:
letters (in here there may be letters, numbers and whitespace)
I have try for this but it doesn’t work.
Scanner cin = new Scanner(System.in);
String format = "^[a-zA-Z]* ([a-zA-Z_0-9\\s]*)$";
String userInput = cin.nextLine();
if (userInput.matches(format)) {
System.out.println("Correct Patten");
} else {
System.out.println("Incorrect Pattern");
}
Thanks in advance…
Some characters in regex has special meaning, parenthesis is one of them. For them ot be interpreted as a character you need to escape them with a backslash, and since backslash has a special meaning in java strings you need to escape that with a backslash (for a total of two).
So your regex should be