Can anyone explain to me why my regular expression:
[a-zA-Z]+\s(.+?)+\s(?:pr.|g.|a.)|[A-Z][a-zA-Z]+\.\s[a-zA-Z+\s(?:pr.|g.|a.)|[a-zA-Z]+\s(?:pr.|g.|a.)|[a-zA-Z]+\s[a-zA-Z]+\s(?:pr.|g.|a.)
With input:
Testės r. sav., Žtestčių Vhest sen., Platakių k.
is causing catastrophic Backtracking?
On the other hand, if I add ^ and $, like:
^[a-zA-Z]+\s(.+?)+\s(?:pr.|g.|a.)|[A-Z][a-zA-Z]+\.\s[a-zA-Z+\s(?:pr.|g.|a.)|[a-zA-Z]+\s(?:pr.|g.|a.)|[a-zA-Z]+\s[a-zA-Z]+\s(?:pr.|g.|a.)$
There is no backtracking.
(tested with regex101.com)
However, when I try to use regular expression (^$ included) with Java:
Matcher matcher = myRegexPattern.matcher(string);
if(matcher.find()){
//
}
Backtrack persists... Any ideas how to avoid backtrack using java?