https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112017-09-11T09:17:11ZRuby Issue Tracking SystemRuby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactershttps://bugs.ruby-lang.org/issues/13890?journal_id=666012017-09-11T09:17:11ZEregon (Benoit Daloze)
<ul></ul><p>Should it behave the same as str.scan(regexp).size ?</p>
<p>I think the default should be no overlap, and increment the position by the length of the match.</p> Ruby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactershttps://bugs.ruby-lang.org/issues/13890?journal_id=666022017-09-11T09:35:00Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>Eregon (Benoit Daloze) wrote:</p>
<blockquote>
<p>I think the default should be no overlap, and increment the position by the length of the match.</p>
</blockquote>
<p>That would be fine by me, too.</p> Ruby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactershttps://bugs.ruby-lang.org/issues/13890?journal_id=690972017-12-30T04:37:33Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>Python allows to count strings, as follows:</p>
<p><code>str.count(sub[, start[, end]])</code><br>
Return the number of non-overlapping occurrences of <code>substring</code> <code>sub</code> in the range <code>[start, end]</code>. Optional arguments <code>start</code> and <code>end</code> are interpreted as in slice notation.</p> Ruby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactershttps://bugs.ruby-lang.org/issues/13890?journal_id=749262018-11-19T04:50:24Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-7 priority-4 priority-default closed" href="/issues/12698">Feature #12698</a>: Method to delete a substring by regex match</i> added</li></ul> Ruby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactershttps://bugs.ruby-lang.org/issues/13890?journal_id=1007412022-12-21T20:04:03Zshan (Shannon Skipper)
<ul></ul><p>I'd love to have this feature. A <code>str.count(regexp)</code> is something I see folk trying fairly often. A <code>str.count(regexp)</code> also avoids the intermediary Array of <code>str.scan(regexp).size</code> or the back bending with <code>str.enum_for(:scan, regexp).count</code>.</p> Ruby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactershttps://bugs.ruby-lang.org/issues/13890?journal_id=1013152023-01-19T05:20:52Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>If <code>str.count(re)</code> works as <code>str.scan(re).size</code> (besides efficiency), it's acceptable. But if someone needs overlapping, they needs to explain their use-case.</p>
<p>Matz.</p> Ruby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactershttps://bugs.ruby-lang.org/issues/13890?journal_id=1013242023-01-19T08:59:21Zsawa (Tsuyoshi Sawada)
<ul></ul><p>Overlapping can be realized by putting the original regexp within a look-ahead.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">s</span> <span class="o">=</span> <span class="s2">"abcdefghij"</span>
<span class="n">re</span> <span class="o">=</span> <span class="sr">/.{3}/</span>
</code></pre>
<p>Non-overlapping count:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">s</span><span class="p">.</span><span class="nf">scan</span><span class="p">(</span><span class="n">re</span><span class="p">).</span><span class="nf">count</span> <span class="c1"># => 3</span>
<span class="n">s</span><span class="p">.</span><span class="nf">count</span><span class="p">(</span><span class="n">re</span><span class="p">)</span> <span class="c1"># => Expect 3</span>
</code></pre>
<p>Overlapping count:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">s</span><span class="p">.</span><span class="nf">scan</span><span class="p">(</span><span class="sr">/(?=</span><span class="si">#{</span><span class="n">re</span><span class="si">}</span><span class="sr">)/</span><span class="p">).</span><span class="nf">count</span> <span class="c1"># => 8</span>
<span class="n">s</span><span class="p">.</span><span class="nf">count</span><span class="p">(</span><span class="sr">/(?=</span><span class="si">#{</span><span class="n">re</span><span class="si">}</span><span class="sr">)/</span><span class="p">)</span> <span class="c1"># => Expect 8</span>
</code></pre>
<p>So I do not think there is any need to particularly implement overlapping as a feature of this method.</p>