Possessive generic quantifier {m,n}+ not implemented in Ruby 1.9.3?
oniguruma, regex, ruby
Solution
It seems like this is intended in Oniguruma. Documentation says `{n,m}+, {n,}+, {n}+ are possessive op. in ONIG_SYNTAX_JAVA only`. I guess this is because of backward compatibility reasons, or?
Problem
Possessive quantifiers are greedy and refuse backtrack. A regex `/.{1,3}+b/` should mean: Match any character except line breaks, 1 to 3 times, as many as possible and don't backtrack. Tthen match the character `b`. In this example: ``` 'ab'.sub /.{1,3}+b/, 'c' #=> "c" ``` no substitution should take place, contrary to fact. The result in these two examples differs: ``` 'aab'.sub /.{0,1}+b/, 'c' #=> "c" 'aab'.sub /.?+b/, 'c' #=> "ac" ``` Compare this with Scala, where they give the same answer: ``` scala> ".{0,1}+b".r.replaceAllIn("aab", "c") res1: String = ac scala> ".?+b".r.replaceAllIn("aab", "c") res2: String = ac ``` Is this a Ruby bug, or is it possible to motivate this behavior? Perhaps, Oniguruma for some reason implemented possessive with all quantifiers `?`, `*`, `+` except the generic quantifier `{m,n}`? If that's the case, why?