Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-02-01T15:28:00ZRuby Issue Tracking System
Redmine Ruby master - Bug #15583 (Closed): Regex: ? on quantified group {n} is interpreted as optional, s...https://bugs.ruby-lang.org/issues/155832019-02-01T15:28:00Zdavisjam (James Davis)davisjam@vt.edu
<p>The Ruby regex docs have this to say about repetition (<a href="https://ruby-doc.org/core-2.6.1/Regexp.html#class-Regexp-label-Repetition" class="external">specific link</a>):</p>
<blockquote>
<p>The constructs described so far match a single character. They can be followed by a repetition metacharacter to specify how many times they need to occur. Such metacharacters are called quantifiers.</p>
<ul>
<li>
<ul>
<li>
<ul>
<li>Zero or more times</li>
</ul>
</li>
</ul>
</li>
<li>...</li>
<li>{n} - Exactly n times</li>
<li>...</li>
</ul>
</blockquote>
<p>From this I conclude that the {n} construct is considered a quantifier metacharacter.</p>
<p>The docs go on to say</p>
<blockquote>
<p>Repetition is greedy by default: as many occurrences as possible are matched while still allowing the overall match to succeed. By contrast, lazy matching makes the minimal amount of matches necessary for overall success. A greedy metacharacter can be made lazy by following it with ?.</p>
</blockquote>
<p>Since <code>{n}</code> is a greedy metacharacter, it seems like <code>{n}?</code> should make it lazy. In the particular case of <code>{n}?</code>, laziness is meaningless -- the regex engine must match n of whatever is being quantified, lazily or not. But I think other behavior is needlessly confusing. To make <code>{n}</code> optional, I think I should have to wrap it in parentheses: <code>(a{n})?</code>.</p>
<p>The docs make it sound like <code>?</code> as "lazy" has stronger precedence than <code>?</code> as "optional".<br>
This make sense to me -- the "optional" meaning can be communicated using parentheses while the lazy meaning cannot.</p>
<p>Here is a test program to explore this behavior:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="sr">/a{1,}?/</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"a{1,}? matched the empty string"</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s2">"a{1,}? did not match"</span>
<span class="k">end</span>
<span class="k">if</span> <span class="sr">/a{1,3}?/</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"a{1,3}? matched the empty string"</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s2">"a{1,3}? did not match"</span>
<span class="k">end</span>
<span class="k">if</span> <span class="sr">/a{,1}?/</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"a{,1}? matched the empty string"</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s2">"a{,1}? did not match"</span>
<span class="k">end</span>
<span class="k">if</span> <span class="sr">/a{1}?/</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"a{1}? matched the empty string"</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s2">"Did not match"</span>
<span class="k">end</span>
</code></pre>
<p>If <code>?</code> attaches more strongly to quantifers (to mean non-greedy) than to arbitrary patterns (to mean optional), then I expect it to mean "non-greedy" in each of these cases. So the expected behavior is:</p>
<ol>
<li>
<code>/a{1,}?/</code> <em>should not</em> match the empty string, since even non-greedily it must match at least 1 a.</li>
<li>
<code>/a{1,3}?/</code> <em>should not</em> match the empty string, since even non-greedily it must match at least 1 a.</li>
<li>
<code>/a{,1}?/</code> <em>should</em> match the empty string, since non-greedily it can match 0 a's.</li>
<li>
<code>/a{1}?/</code> <em>should not</em> match the empty string, since even non-greedily it must match at least 1 a.</li>
</ol>
<p>Let's see how it behaves in Ruby 2.6.1:</p>
<pre><code class="shell syntaxhl" data-language="shell"><span class="o">(</span>09:43:09<span class="o">)</span> jamie@woody /tmp <span class="nv">$ </span>ruby <span class="nt">-v</span>
ruby 2.6.1p33 <span class="o">(</span>2019-01-30 revision 66950<span class="o">)</span> <span class="o">[</span>x86_64-linux]
<span class="o">(</span>09:43:12<span class="o">)</span> jamie@woody /tmp <span class="nv">$ </span>ruby /tmp/t.rb
a<span class="o">{</span>1,<span class="o">}</span>? did not match
a<span class="o">{</span>1,3<span class="o">}</span>? did not match
a<span class="o">{</span>,1<span class="o">}</span>? matched the empty string
a<span class="o">{</span>1<span class="o">}</span>? matched the empty string
</code></pre>
<p>Cases 1-3 all behave as expected. However, case 4 matches the empty string, implying that in <code>/a{1}?/</code> the <code>?</code> interpreted to mean optional rather than non-greedy.<br>
I find this inconsistency a bit confusing.</p>
<p>I tested this behavior in 7 other languages: Go, Java, JavaScript, Perl, PHP, Python, and Rust. In those languages, /a{1}?/ does not match the empty string (and is thus the <code>{n}?</code> notation interpreted as non-greedy rather than optional).</p>
<p>Perhaps this should be addressed via a docs change to avoid possible breakage. Here is some possible wording:</p>
<p>Repetition is greedy by default: as many occurrences as possible are matched while still allowing the overall match to succeed. By contrast, lazy matching makes the minimal amount of matches necessary for overall success. Most greedy metacharacters can be made lazy by following them with ?. For the {n} metacharacter, greedy and non-greedy behavior is identical and the ? instead makes the repeated pattern optional.</p>