Just a quick question, I’m writing a regex to allow ONLY letters (so just a words) and the following regex allows spaces, though I have not included them so why are they included?
([a-zA-Z])+
I’ve had to change it to this (pasted from Java):
([a-zA-Z]&&[^\\s])+
To provide context – I’ve tried this and input words with spaces and it still prints true:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter: ");
String s = in.next();
if (s.matches("[a-zA-Z]+")) {
System.out.println("TRUE");
}
}
This will always print TRUE as
uses the first token input by the user (delimited by space by Scanner).
Better to use: