String input = scanner.nextLine();
Pattern pattern = Pattern.compile("the [a-z]+ jumped over the [a-z]+ ")
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
// how do I print out what jumped over what???
}
In this example, someone will type something like “the cow jumped over the moon” or
“the fox jumped over the dog” or “the cat jumped over the mouse” …
I will need to be able to figure out what values they put into the two placeholders.
So my question is how do I get the values of the two [a-z]+ spots in the regex.
You use capturing groups which are marked by parantheses:
"the ([a-z]+) jumped over the ([a-z]+)".Then use
matcher.group(1)andmatcher.group(2)to retrieve them (group 0 is always the entire match).