snipt

Ctrl+h for KB shortcuts

C#

Cyclic detection using RegExp - detect a sequence of numbers which is repeated (at least once) in a given line of numbers

    //	Very cool compact piece of code to detect a sequence that repeats at least
    //	once.  I have seen some solutions that are a lot more complicated, however
    //	using RegExp is very cool.
    
    class Program
    {
        static void Main(string[] args)
        {
            string sequence = "2 0 3 6 2 1 5 5 5 1 9 8 4 5 5 5 2 4 3 8 6 3 1 5 5 5 5 5 5";
            string regexp = @"(?'cycle'(\d+\s)+(\d+))\s\k'cycle'";
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(sequence, regexp);
            if (m.Success)
            {
                Console.WriteLine(m.Groups["cycle"]);
            }
            Console.ReadKey();
        }
    }
https://snipt.net/embed/2d1d1113dc0cc4d4bf8f9a5503f16be6/
/raw/2d1d1113dc0cc4d4bf8f9a5503f16be6/
2d1d1113dc0cc4d4bf8f9a5503f16be6
csharp
C#
18
2019-06-20T02:56:08
True
False
False
May 15, 2013 at 11:45 AM
/api/public/snipt/61449/
cyclic-detection-using-regexp-detect-a-sequence-of-numbers-which-is-repeated-at-least-once-in-a-given-line-of-numbers
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a> <span class="c1">// Very cool compact piece of code to detect a sequence that repeats at least</span> </span><span id="L-2"><a name="L-2"></a> <span class="c1">// once. I have seen some solutions that are a lot more complicated, however</span> </span><span id="L-3"><a name="L-3"></a> <span class="c1">// using RegExp is very cool.</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a> <span class="k">class</span> <span class="nc">Program</span> </span><span id="L-6"><a name="L-6"></a> <span class="p">{</span> </span><span id="L-7"><a name="L-7"></a> <span class="k">static</span> <span class="k">void</span> <span class="nf">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span> </span><span id="L-8"><a name="L-8"></a> <span class="p">{</span> </span><span id="L-9"><a name="L-9"></a> <span class="kt">string</span> <span class="n">sequence</span> <span class="p">=</span> <span class="s">&quot;2 0 3 6 2 1 5 5 5 1 9 8 4 5 5 5 2 4 3 8 6 3 1 5 5 5 5 5 5&quot;</span><span class="p">;</span> </span><span id="L-10"><a name="L-10"></a> <span class="kt">string</span> <span class="n">regexp</span> <span class="p">=</span> <span class="s">@&quot;(?&#39;cycle&#39;(\d+\s)+(\d+))\s\k&#39;cycle&#39;&quot;</span><span class="p">;</span> </span><span id="L-11"><a name="L-11"></a> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">RegularExpressions</span><span class="p">.</span><span class="n">Match</span> <span class="n">m</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">RegularExpressions</span><span class="p">.</span><span class="n">Regex</span><span class="p">.</span><span class="n">Match</span><span class="p">(</span><span class="n">sequence</span><span class="p">,</span> <span class="n">regexp</span><span class="p">);</span> </span><span id="L-12"><a name="L-12"></a> <span class="k">if</span> <span class="p">(</span><span class="n">m</span><span class="p">.</span><span class="n">Success</span><span class="p">)</span> </span><span id="L-13"><a name="L-13"></a> <span class="p">{</span> </span><span id="L-14"><a name="L-14"></a> <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">m</span><span class="p">.</span><span class="n">Groups</span><span class="p">[</span><span class="s">&quot;cycle&quot;</span><span class="p">]);</span> </span><span id="L-15"><a name="L-15"></a> <span class="p">}</span> </span><span id="L-16"><a name="L-16"></a> <span class="n">Console</span><span class="p">.</span><span class="n">ReadKey</span><span class="p">();</span> </span><span id="L-17"><a name="L-17"></a> <span class="p">}</span> </span><span id="L-18"><a name="L-18"></a> <span class="p">}</span> </span></pre></div> </td></tr></table>