What really happens
It seems that +
followed by range quantifier doesn't offer the possessive property to the range quantifier. Rather, it is treated as whatever in front repeated once or more. Using .{1,3}+b
as an example, it will be equivalent to (?:.{1,3})+b
.
Work-around
You can work-around this with the more general construct non-backtracking group (or atomic grouping) (?>pattern)
. Let us use the general case pattern{n,m}+
as an example to construct the equivalent regex with non-backtracking group (equivalent to Java's behavior when matching with pattern{n,m}+
):
(?>(?>pattern){n,m})
Why 2 levels of non-backtracking groups? 2 are necessary because:
- When a match is found for
pattern
(one instance of repetition), backtracking within pattern
is disallowed. (Note that as long as an instance has not been found, backtracking within pattern
is allowed). This is emulated with the inner non-backtrakcing group.
- When no more instances of
pattern
can be found, backtracking to remove any of the instances is disallowed. This is emulated with the outer non-backtracking group.
I am not sure if there is any caveat here. Please ping me with comment if you found any case not emulated with this method.
Testing
Test 1
At first, I tested this regex:
(.{1,3}+)b
Initially, I tested without the capturing group, but the result was so surprising that I need the capturing group to confirm what is happening.
On this input:
2343333ab
The result is that the whole string matches, and the capturing group captured 2343333a
(without the b
at the end). This shows that the upper limit has somehow been broken.
DEMO at rubular
Test 2
This second test reveals how range quantifiers {n}
's behavior cannot be modified to be possessive, and it is likely that this also applies for other range quantifiers {n,}
and {n,m}
. Instead, the following +
will only exhibit repetition of 1 or more time behavior.
(My initial conclusion is that +
overwrites the upper limit, but it turns out to be wrong).
Testing regex:
(.{3}+)b
Input string:
23d4344333ab
234344333ab
23434433ab
The matches captured in capturing group 1 are all multiples of 3. From top to bottom, the regex skips 2, 1, 0 characters respectively for the input strings.
Input string with annotation ([]
denotes the match for the whole regex, ()
denotes the text captured by capturing group 1):
23[(d4344333a)b]
2[(34344333a)b]
[(23434433a)b]
DEMO at rubular
Testing code for work around
This is the testing code in Java to show that both outer and inner non-backtracking groups are necessary. ideone
class TestPossessive {
public static void main(String args[]) {
String inputText = "123456789012";
System.out.println("Input string: " + inputText);
System.out.println("Expected: " + inputText.replaceFirst("(?:\\d{3,4}(?![89])){2,}+", ">$0<"));
System.out.println("Outer possessive group: " + inputText.replaceFirst("(?>(?:\\d{3,4}(?![89])){2,})", ">$0<"));
System.out.println("Inner possessive group: " + inputText.replaceFirst("(?>\\d{3,4}(?![89])){2,}", ">$0<"));
System.out.println("Both: " + inputText.replaceFirst("(?>(?>\\d{3,4}(?![89])){2,})", ">$0<"));
System.out.println();
inputText = "aab";
System.out.println("Input string: " + inputText);
System.out.println("Expected: " + inputText.replaceFirst(".{1,3}+b", ">$0<"));
System.out.println("Outer possessive group: " + inputText.replaceFirst("(?>.{1,3})b", ">$0<"));
System.out.println("Inner possessive group: " + inputText.replaceFirst("(?>.){1,3}b", ">$0<"));
System.out.println("Both: " + inputText.replaceFirst("(?>(?>.){1,3})b", ">$0<"));
}
}