In a regex OR, When there are multiple inputs with a common prefix, The regex will match the first input in Regex OR
instead of longest match.
For example, for the regular expression regex = (KA|KARNATAKA)
and input = KARNATAKA
the output will be 2 matches match1 =KA
and match2 = KA
.
But what I want is complete longest possible match out of given input in Regex OR
which is match1 = KARNATAKA
in my given example.
Here is the example in a regex client
So what I am doing right now is, I am sorting the input in Regex OR
by length in descending order.
My question is, Can we specify in the regex itself to match the longest possible String? Or is sorting the only way to do it?
I have already refered this question and I don't see a solution other than sorting