I have this:
import java.util.regex.*; String regex = "(?<m1>(hello|universe))|(?<m2>(hello world))"; String s = "hello world"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s); while(matcher.find()) { MatchResult matchResult = m.toMatchResult(); String substring = s.substring(matchResult.start(), matchResult.end()); System.out.println(substring); } The above only prints hello whereas I want it to print hello world.
One way to fix this is to re-order the groups in String regex = "(?<m2>(hello world))|(?<m1>(hello|universe))" but I don't have control over the regex I get in my case...
So what is the best way to find the longest match? An obvious way would be to check all possible substrings of s as mentioned here (Efficiently finding all overlapping matches for a regular expression) by length and pick the first but that is O(n^2). Can we do better?
"(?<m1>hello world)|(?<m2>hello|universe)", put the longest alternative branch before the shortest.