I'm doing something in Java that requires input to be matched against the pattern ^[1-5]$. I should have a while loop looping through each line of input, checking it against the pattern, and outputting an error message if it does not.
Sudo code:
while (regex_match(/^[^1-5]$/,inputLine)) {
print ("Please enter a number between 1 and 5! ");
getNextInputLine(); }
I can use java.util.Scanner.hasMatch("^[^1-5]$")
, but that will only match a single token, not the entire line. Any idea on how to make hasMatch match against the entire line? (Setting the delimiter to "\n" or "\0" doesn't work.)
Edit: If this isn't possible, is there some other way to do it?