I have a problem with checking the string and pattern in Java.
I want to check if the string contains at least one word (and check most of them) from the list e.g. [January, February, March] in any order
and group the part of the string before the next same pattern (repeated this again and again until the end of the string).
// months = { "January", "December" } - to simplify, we have only two months
String str = "January I have a problem December There is no more sun";
In the example above I would finally select the following groups:
I have a problem(starts withJanuary)There is no more sun(starts withDecember)
I can't figure out how to define a pattern to check until the next the same pattern occurs in the string. This is my last (not working) solution:
Pattern pattern = Pattern.compile("[\\\bJanuary\\\b|\\\bDecember\\\b]\\s+(.+)");
Matcher matcher = pattern.matcher(str);
while(matcher.find()) {
System.out.println(matcher.group());
}
// should print:
// I have a problem
// There is no more sun