I want to match multiple patterns in given input Strings, so the outcome would be a list containing all the substrings that match any of my predefined patterns:
String input = "Episode_NN 3_CD was_XX awesome_XX";
final Pattern ruleOne = Pattern.compile("(\\w*_NN\\s|\\w*_NNS\\s)+\\w*_CD");
final Pattern ruleTwo = Pattern.compile(ruleOne.pattern().concat(""));
Matcher matcher = ruleOne.matcher(input);
List<String> ent = new ArrayList<String>();
while (matcher.find()) {
ent.add(matcher.group());
}
So do I have to add multiple Matchers? That would mean scanning the text multiple times, like so:
while (matcherOne.find() | matcherTwo.find() | ...) {
....
}
Yes, it's that simple. Except you're better off using the CONDITIONAL OR statement. This evaluates from left to right and if the first condition is true, it never evaluates the rest. It is made up of two bars like so:
||