Reputation: 3140
I'm trying to create a Matcher instance to pull tokens out of a string. This is what I used:
Matcher base = Pattern.compile("red|green|blue|\\+|\\(|\\)").matcher(input.trim());
while (!base.hitEnd()) {
if (base.find()) {
String s = base.group();
output += String.format(" %s", s);
}
else {
throw new IllegalArgumentException("Invalid tokens in the input! " + base.toString());
}
}
In this case input
is my input string to be tokenized. However, even if I give it the input "red"
, it still throws the exception, and shows that the object attempted no matches (no change to the indices being considered, no prior matches).
My goal is to match the exact words "red", "green", "blue"
, the plus sign and the opening and closing parens, as tokens. What am I missing?
Upvotes: 0
Views: 59
Reputation: 1119
If I understand correctly you want to throw your exception when none of the tokens you are looking for are found. This modification of what you had will properly find the tokens you are looking for and throw an exception if the input string did not contain any of the tokens.
Matcher base = Pattern.compile("\\bred\\b|\\bgreen\\b|\\bblue\\b|[+()]{1}").matcher(input.trim());
while (!base.hitEnd()) {
if (base.find()) {
String s = base.group();
System.out.println("Found: " + s);
output += String.format(" %s", s);
}
}
if (output.isEmpty()) {
throw new IllegalArgumentException("Invalid input no matching tokens found! " + base.toString());
}
I updated a few things in your regex. I added \\b
for word boundaries around red, green, blue
and I combined the +()
into a character group. The character group will match exactly 1 of any of the characters within the []
.
Upvotes: 1