I know that java regex does not support varying length look-behinds, and that the following should cause an error
(?<=(not exceeding|no((\\\\w|\\\\s)*)mor
That is indeed strange. I don't find explanation but problem seems to disappear if you change (\\w|\\s){0,30}
to [\\w\\s]{0,30}
Pattern.compile("(?<=(not exceeding|no([\\w\\s]{0,30})more than))xxxx");
//BTW you don't need ^-----------------------------------------^ these parenthesis
//unless you want to use match from this group later
So you thought Java did not support infinite lookbehind?
But the following pattern will compile!
(?<=\d+)\w+
...though in a Match All it will yield unexpected results (see demo).
On the other hand, you can with success use this other infinite lookbehind (which I found with great surprise on this question)
(?<=\\G\\d+,\\d+,\\d+),
to split this string: 0,123,45,6789,4,5,3,4,6000
It will correctly output (see the online demo):
0,123,45
6789,4,5
3,4,6000
This time the results are what you expect.
But if you tweak the regex the slightest bit to obtain pairs instead of triplets, with (?<=\\G\\d+,\\d+),
, this time it will not split (see the demo).
Java lookbehind is notoriously buggy. Knowing this, I recommend you don't waste time trying to understand why it does something that is undocumented.
The decisive words that drove me to this conclusion some time ago are those from Jan Goyvaerts, who is a co-author of The Regex Cookbook and an arch-regex-guru who has created a terrific regex engine and needs to stay on top of most regex flavors under the sun for his debugging tool RegexBuddy:
Java has a number of bugs in its lookbehind implementation. Some (but not all) of those were fixed in Java 6.
Below are some test cases (I removed the redundant parens, as mentioned by @Pshemo). It only fails where the lookbehind contains a sub-alternation. The error is
Look-behind group does not have an obvious maximum length near index 45
"Obvious" being the keyword here.
import java.util.regex.Pattern;
public class Test {
public static final void main(String[] ignored) {
test("(?<=not exceeding|no)xxxx");
test("(?<=not exceeding|NOT EXCEEDING)xxxx");
test("(?<=not exceeding|x{13})xxxx");
test("(?<=not exceeding|x{12}x)xxxx");
test("(?<=not exceeding|(x|y){12}x)xxxx");
test("(?<=not exceeding|no(\\w|\\s){2,30}more than)xxxx");
test("(?<=not exceeding|no(\\w|\\s){0,2}more than)xxxx");
test("(?<=not exceeding|no(\\w|\\s){2}more than)xxxx");
}
private static final void test(String regex) {
System.out.print("testing \"" + regex + "\"...");
try {
Pattern p = Pattern.compile(regex);
System.out.println("Success");
} catch(Exception x) {
System.out.println(x);
}
}
}
Output:
testing "(?<=not exceeding|no)xxxx"...Success
testing "(?<=not exceeding|NOT EXCEEDING)xxxx"...Success
testing "(?<=not exceeding|x{13})xxxx"...Success
testing "(?<=not exceeding|x{12}x)xxxx"...Success
testing "(?<=not exceeding|(x|y){12}x)xxxx"...java.util.regex.PatternSyntaxException: Look-behind group does not
have an obvious maximum length near index 27
(?<=not exceeding|(x|y){12}x)xxxx
^
testing "(?<=not exceeding|no(\w|\s){2,30}more than)xxxx"...java.util.regex.PatternSyntaxException: Look-behind
group does not have an obvious maximum length near index 41
(?<=not exceeding|no(\w|\s){2,30}more than)xxxx
^
testing "(?<=not exceeding|no(\w|\s){0,2}more than)xxxx"...java.util.regex.PatternSyntaxException: Look-behind g
roup does not have an obvious maximum length near index 40
(?<=not exceeding|no(\w|\s){0,2}more than)xxxx
^
testing "(?<=not exceeding|no(\w|\s){2}more than)xxxx"...java.util.regex.PatternSyntaxException: Look-behind gro
up does not have an obvious maximum length near index 38
(?<=not exceeding|no(\w|\s){2}more than)xxxx
^
java regex does not support varying length look-behinds
It is not totally true, Java supports limited variable length lookbehinds, example (?<=.{0,1000})
is allowed or something like (?<=ab?)c
or (?<=abc|defgh)
.
But if there is no limit at all, Java doesn't support it.
So, what is not obvious for the java regex engine for a lookbehind subpattern:
a {m,n}
quantifier applyed to a non-fixed length subpattern:
(?:abc){0,1} is allowed
(?:ab?)? is allowed
(?:ab|de) is allowed
(?:ab|de)? is allowed
(?:ab?){0,1} is not allowed
(?:ab|de){1} is not allowed
(?:ab|de){0,1} is not allowed # in my opinion, it is because of the alternation.
# When an alternation is detected, the analysis
# stops immediatly
To obtain this error message in this particular kind of cases, you need two criterae:
a potentially variable length subpattern (ie: that contains a quantifier, an alternation or a backreference)
and a {m,n}
type quantifier.
All these cases don't seem evident for the user, since it seems like an arbitrary choice. However, I think that the real reason is to limit the pre-analysis time of the pattern by the regex engine transmission.