I wrote this regex:
String regex = "^\\s+something\\s+(not\\s+)?else\\s+([-,\\s\\d]+)$";
and this code:
String line = "something else 1, 2-3";
Matcher m = Pattern.compile(regex).matcher(line);
m.matches() will return true, which is what I want.
However, for the following piece of code, it also returns true, but I don’t want it to match. How do I modify my regex so that it returns false?
String line = "something else 1 2 3";
Matcher m = Pattern.compile(regex).matcher(line);
Thanks!
Update: I’m not doing any range checking. I just want to see if there are commas between spaces in a list. So 1, 3, 5-7 are valid, but 1, 2 5-7 and 1 2 3 are not.
You didn’t exactly say what you were trying to do, but if I were to guess, something like:
Substituted for the last bit would only allow comma separated numbers and number lists