<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Repeated capturing groups]]></title><description><![CDATA[<p dir="auto">Is it possible to reference a captured group that repeated multiple times. For example,</p>
<pre><code>^insert into[ \t]([-_.:/&amp;a-zAZ0-9]+)[ \t]*[(](?:([-_.:/&amp;a-zAZ0-9]+)[,]*){1,5}[)]
</code></pre>
<p dir="auto">will match all of the following.</p>
<pre><code>INSERT INTO mine (countrycode,statecode,id,statename,sort)
</code></pre>
<p dir="auto">Is there a way I can reference the individual matches (like countrycode or id)?  If I use ‘$2’, I get the last match of sort.  The results for $3, $4, and $5 are empty.  I would like to capture and reference the individual matches without having to repeat the same regular expression.</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/26034/repeated-capturing-groups</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 04:06:25 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/26034.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Aug 2024 23:17:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Repeated capturing groups on Tue, 20 Aug 2024 23:36:56 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/joe-mccay" aria-label="Profile: joe-mccay">@<bdi>joe-mccay</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/coises" aria-label="Profile: coises">@<bdi>coises</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: mark-olson">@<bdi>mark-olson</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/mkupper" aria-label="Profile: mkupper">@<bdi>mkupper</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">Ah… yes, the <a class="plugin-mentions-user plugin-mentions-a" href="/user/mkupper" aria-label="Profile: mkupper">@<bdi>mkupper</bdi></a>’s formulation of the <strong>search</strong> regex is very <strong>clever</strong> and quite clear, thanks to the <strong>free-spacing</strong> mode !</p>
<hr />
<p dir="auto">I particularly like :</p>
<ul>
<li>
<p dir="auto">The <strong><code>(?:,((?1)))?</code></strong> syntax, where you join the <strong>optional</strong> states of, both, the <strong><code>(?1)</code></strong> form and the <strong>comma</strong></p>
</li>
<li>
<p dir="auto">The use of the <strong>leading</strong> <strong><code>i</code></strong> modifier to simplify the <strong>group <code>1</code></strong> syntax</p>
</li>
</ul>
<p dir="auto">Bravo !!</p>
<p dir="auto">BR</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96240</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96240</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 20 Aug 2024 23:36:56 GMT</pubDate></item><item><title><![CDATA[Reply to Repeated capturing groups on Tue, 20 Aug 2024 18:47:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/joe-mccay" aria-label="Profile: Joe-McCay">@<bdi>Joe-McCay</bdi></a> I’m going to re-do <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a>’s solution a little to make it something that seemed a little more understandable to me…</p>
<pre><code class="language-txt">(?xi)                    # (?x) Enables free-spacing mode which allows me to spread the expression over several lines and allows for # prefixed comments. (?i) enabled ignore-case mode so that [a-z] also matches [A-Z]
    ^insert\ into[\ \t]  # Due to free-spacing mode we need a backslash in front of spaces that we want to be part of the match pattern
    ([-_.:/&amp;a-z0-9]+)    # I removed the seemingly spurious "AZ" you had which is also not needed as we are in ignore-case mode
    [\ \t]*
    \(
    ((?1))               # This reuses the $1 regexp to match the first parameter of the INSERT INTO
    (?:,((?1)))?         # The second up through fifth parameters are optional with all of them also reusing the $1 regexp
    (?:,((?1)))?
    (?:,((?1)))?
    (?:,((?1)))?
    \)
</code></pre>
<p dir="auto">Look for <code>free-spacing</code> on <a href="https://npp-user-manual.org/docs/searching/#search-modifiers" rel="nofollow ugc">https://npp-user-manual.org/docs/searching/#search-modifiers</a> to see how the <code>(?x)</code> and <code>(?i)</code> things work.</p>
<p dir="auto">Look for <code>subexpression</code> on <a href="https://npp-user-manual.org/docs/searching/" rel="nofollow ugc">https://npp-user-manual.org/docs/searching/</a> to see how the <code>(?ℕ)</code> thing works. Subexpressions were used by both <a class="plugin-mentions-user plugin-mentions-a" href="/user/coises" aria-label="Profile: Coises">@<bdi>Coises</bdi></a> and <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> and are key to doing what you want to do.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96235</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96235</guid><dc:creator><![CDATA[mkupper]]></dc:creator><pubDate>Tue, 20 Aug 2024 18:47:49 GMT</pubDate></item><item><title><![CDATA[Reply to Repeated capturing groups on Tue, 20 Aug 2024 18:01:05 GMT]]></title><description><![CDATA[<p dir="auto">C# <code>System.Text.RegularExpressions</code> supports repeated capture groups, so in principle someone could build a C# plugin that does regex search with repeated capture groups.</p>
<p dir="auto">I could even add support for repeated capture groups to the regex search functionalities of the JsonTools plugin, since it is implemented in C#. I’m just not currently sure what would be the most user-friendly way to do that.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96234</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96234</guid><dc:creator><![CDATA[Mark Olson]]></dc:creator><pubDate>Tue, 20 Aug 2024 18:01:05 GMT</pubDate></item><item><title><![CDATA[Reply to Repeated capturing groups on Tue, 20 Aug 2024 16:05:52 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/joe-mccay" aria-label="Profile: joe-mccay">@<bdi>joe-mccay</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/coises" aria-label="Profile: coises">@<bdi>coises</bdi></a> and <strong>all</strong>,</p>
<p dir="auto">I found out a solution, similar to the <a class="plugin-mentions-user plugin-mentions-a" href="/user/coises" aria-label="Profile: coises">@<bdi>coises</bdi></a>’s one, which seems <strong>slightly</strong> easier to understand :</p>
<p dir="auto">SEARCH <strong><code>(?i-s)^insert into[ \t]([-_.:/&amp;a-zAZ0-9]+)[ \t]*\(((?1)),?((?1)?),?((?1)?),?((?1)?),?((?1)?)\)</code></strong></p>
<p dir="auto">From this <em>INPUT</em> text, below :</p>
<pre><code class="language-diff">INSERT INTO mine (countrycode,statecode,id,statename,sort)
INSERT INTO mine (countrycode,statecode,id,statename)
INSERT INTO mine (countrycode,statecode,id)
INSERT INTO mine (countrycode,statecode)
INSERT INTO mine (countrycode)
</code></pre>
<p dir="auto">The following <strong>regex</strong> S/R :</p>
<p dir="auto">SEARCH <strong><code>(?i-s)^insert into[ \t]([-_.:/&amp;a-zAZ0-9]+)[ \t]*\(((?1)),?((?1)?),?((?1)?),?((?1)?),?((?1)?)\)</code></strong></p>
<p dir="auto">REPLACE <strong><code>&gt;$1&lt;    &gt;$2&lt;    &gt;$3&lt;    &gt;$4&lt;    &gt;$5&lt;    &gt;$6&lt;</code></strong></p>
<p dir="auto">Would produce this <em>OUTPUT</em> text :</p>
<pre><code class="language-diff">&gt;mine&lt;    &gt;countrycode&lt;    &gt;statecode&lt;    &gt;id&lt;    &gt;statename&lt;    &gt;sort&lt;
&gt;mine&lt;    &gt;countrycode&lt;    &gt;statecode&lt;    &gt;id&lt;    &gt;statename&lt;    &gt;&lt;
&gt;mine&lt;    &gt;countrycode&lt;    &gt;statecode&lt;    &gt;id&lt;    &gt;&lt;    &gt;&lt;
&gt;mine&lt;    &gt;countrycode&lt;    &gt;statecode&lt;    &gt;&lt;    &gt;&lt;    &gt;&lt;
&gt;mine&lt;    &gt;countrycode&lt;    &gt;&lt;    &gt;&lt;    &gt;&lt;    &gt;&lt;
</code></pre>
<hr />
<p dir="auto"><strong>Notes</strong> :</p>
<ul>
<li>
<p dir="auto">After the <strong>first</strong> part <strong><code>(?i-s)^insert into[ \t]</code></strong>, the group <strong><code>1</code></strong> is the part <strong><code>[-_.:/&amp;a-zAZ0-9]+</code></strong></p>
</li>
<li>
<p dir="auto">Then, after possible <strong>leading blank</strong> chars and the opening <strong>parenthesis</strong>, the <strong>true</strong> regex <strong><code>(?1)</code></strong> is repeated and surrounded,<strong>itself</strong>, with parentheses to get the <strong>group <code>2</code></strong> and followed with a possible <strong>comma</strong> char <strong><code>,?</code></strong></p>
</li>
<li>
<p dir="auto">Again, the regex <strong><code>(?1)</code></strong> is, this time, <strong>optionally</strong> repeated and enclosed, as before, between parentheses to get the <strong>optional</strong> group <strong><code>3</code></strong></p>
</li>
<li>
<p dir="auto">The whole regex include <strong>three</strong> other ranges <strong><code>,?((?1)?)</code></strong> to cover from possible groups <strong>four</strong> to <strong>six</strong> and ends with the <strong>ending</strong> parenthesis !</p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
<p dir="auto"><strong>P.S.</strong> :</p>
<p dir="auto">See the <strong>fundamental</strong> difference between these <strong>two</strong> regexes :</p>
<p dir="auto"><strong>A</strong> <strong><code>(?-is)(\d+)ABC\1</code></strong></p>
<p dir="auto">and</p>
<p dir="auto"><strong>B</strong> <strong><code>(?-is)(\d+)ABC(?1)</code></strong></p>
<p dir="auto">Given the <em>INPUT</em> text :</p>
<pre><code class="language-diff">1ABC1
12345ABC12345
456ABC456
89ABC89

456ABC789
789ABC456
0ABC123456789
0123456789ABC1
111ABC999
</code></pre>
<p dir="auto">The regex <strong><code>(?-is)(\d+)ABC\1</code></strong>  matches the <strong>first four</strong> lines only of the <em>INPUT</em> text, whereas the regex <strong><code>(?-is)(\d+)ABC(?1)</code></strong>  matches also the <strong>five other</strong> lines, below !</p>
<p dir="auto">Indeed, the regex <strong><code>(?-is)(\d+)ABC(?1)</code></strong> is <strong>identical</strong> to the regex <strong><code>(?-is)(\d+)ABC(\d+)</code></strong>. So, the <strong><code>(?1)</code></strong> syntax is just a <strong>shortcut</strong> to the regex which represents the <strong>whole</strong> group <strong><code>1</code></strong> !</p>
<p dir="auto">But the <strong><code>\1</code></strong> syntax, in the regex <strong>A</strong>, represents the <strong>present</strong> value of group <strong><code>1</code></strong> ( i.e. a <strong>reference</strong> to group <strong><code>1</code></strong> )</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96232</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96232</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 20 Aug 2024 16:05:52 GMT</pubDate></item><item><title><![CDATA[Reply to Repeated capturing groups on Tue, 20 Aug 2024 14:26:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/coises" aria-label="Profile: Coises">@<bdi>Coises</bdi></a> Thanks.  That is what I thought.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96229</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96229</guid><dc:creator><![CDATA[Joe McCay]]></dc:creator><pubDate>Tue, 20 Aug 2024 14:26:56 GMT</pubDate></item><item><title><![CDATA[Reply to Repeated capturing groups on Tue, 20 Aug 2024 00:59:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/joe-mccay" aria-label="Profile: Joe-McCay">@<bdi>Joe-McCay</bdi></a> said in <a href="/post/96226">Repeated capturing groups</a>:</p>
<blockquote>
<p dir="auto">Is there a way I can reference the individual matches (like countrycode or id)? If I use ‘$2’, I get the last match of sort. The results for $3, $4, and $5 are empty. I would like to capture and reference the individual matches without having to repeat the same regular expression.</p>
</blockquote>
<p dir="auto">There is no way to do that using the Notepad++ regular expression implementation.</p>
<p dir="auto">The closest I could come was this:<br />
<strong><code>^insert into[ \t]([-_.:/&amp;a-zAZ0-9]+)[ \t]*[(]([-_.:/&amp;a-zAZ0-9]+)(?:,((?2)))?(?:,((?2)))?(?:,((?2)))?(?:,((?2)))?[)]</code></strong><br />
which isn’t much better than just repeating the expression — though if the actual expression were more complex or subject to change, the technique might help. The problem is that you have to have an actual, written-out pair of parentheses for each numbered capture group; if a parenthesized group matches more than once, only the last match is saved.</p>
<p dir="auto">The Boost regular expression engine which Notepad++ uses has an option for that called <a href="https://www.boost.org/doc/libs/1_86_0/libs/regex/doc/html/boost_regex/captures.html#boost_regex.captures.repeated_captures" rel="nofollow ugc">Repeated Captures</a>, but it is only accessible through the programming interface; there is no support for using it in a replacement string. A plugin could use this feature, but it would have to call its own copy of Boost::regex directly; I don’t know if any of the scripting interfaces can do it.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96227</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96227</guid><dc:creator><![CDATA[Coises]]></dc:creator><pubDate>Tue, 20 Aug 2024 00:59:28 GMT</pubDate></item></channel></rss>