I have a String pattern like this
String pattern = "Send {{message}} to {{name}}";
Than I have a sentence like this one
String sentence = "Send Hi there to Jesus";
What I want is to match sentences with a pattern and return something like a JsonObject that has the arguments from the sentence:
{"message": "Hi there", "name": "Jesus"}
Are there any simple solutions for this ?
This unit test extracts the sentence content by refererencing matched groups (note the enclosing round brackets) via the group index. If the pattern matches the given string, then the entire input string is group 0. In the given example, the matched
message
is at group index 1 and thename
at index 2. Alternatively, you can define named groups.An ad-hoc solution could be to use String.format to insert dynamic capture group names like this: