Given that the user can enter values in specific formats only, I need to extract relevant sections of that string into Java variables.
Say for instance acceptable formats are:-
String types[] = {"The quick brown ${animal} jumped over the lazy ${target}.",
"${target} loves ${animal}.",
"${animal} became friends with ${target}"};
Variables:-
private String animal;
private String target;
Now if the user enters "The quick brown fox jumped over the lazy dog.", the animal variable should be set to "fox" and target variable should be set to "dog".
If the user input matches none of the given types, it should display an error.
Basically I am trying to do the inverse of what org.apache.commons.lang.text.StrSubstitutor does.
My approach (looks inefficient, hence asking for help):-
Create regex patterns to find out the type of the entered string and then write different logic for each of the type. For example, for the first type, get the word after the word "brown" and assign it to variable animal and so on.
appendReplacement, which gives the matched part of the string.