How to check if string consists of letters only by using regular expression.
I know how to check only one character: if (s.matches("\\w")) System.out.println(true);
But I don`t understand how to check full string.
3 Answers
Replace matches("\\w") with matches("^[A-Za-z]*$"). This should instruct the matcher to expect the string to be made up entirely of 0 or more letters (case insensitive).
The * denotes zero or more repetitions of what preceeds it. On the other hand, the + expects at least one instance of whatever it is that preceeds it, thus, the * operator can potentially match an empty string, which I do not know if it is something which you are after.