<?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[How to refer named group in RegExp-replacement?]]></title><description><![CDATA[<p dir="auto">I want to use named group in RegExp to replace it with some text.<br />
I can’t understand how to refer this group in “replace”-textbox.<br />
I tried this way but failed:</p>
<p dir="auto"><img src="/assets/uploads/files/1695655547382-b8f80633-1c1a-42a8-a34d-56347a4f8716-%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5.png" alt="b8f80633-1c1a-42a8-a34d-56347a4f8716-изображение.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://community.notepad-plus-plus.org/topic/24965/how-to-refer-named-group-in-regexp-replacement</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 22:15:47 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/24965.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 25 Sep 2023 15:25:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to refer named group in RegExp-replacement? on Mon, 25 Sep 2023 20:57:38 GMT]]></title><description><![CDATA[<p dir="auto">While we’re here, I’d like to talk about a related useful concept, namely how to reference a <em>named pattern (but not the value captured earlier)</em>, which is extremely useful if you have some annoyingly long pattern that you need to match multiple times in the same regex.</p>
<p dir="auto">For example,<br />
<code>(?'int'[-+]?\d+)\h*b+\h*(?&amp;int)</code><br />
matches an integer with leading + or minus, some <code>b</code> characters, and then another integer (referenced with <code>(?&amp;int)</code>)</p>
<p dir="auto">I won’t expound too much on this topic because it’s also in the documentation, but I’ve definitely written many over-long regexes that could have been hugely simplified by this method.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/89503</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/89503</guid><dc:creator><![CDATA[Mark Olson]]></dc:creator><pubDate>Mon, 25 Sep 2023 20:57:38 GMT</pubDate></item><item><title><![CDATA[Reply to How to refer named group in RegExp-replacement? on Mon, 25 Sep 2023 17:25:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/5329">@mkupper</a> said in <a href="/post/89501">How to refer named group in RegExp-replacement?</a>:</p>
<blockquote>
<p dir="auto">only mentions the two forms that work.</p>
</blockquote>
<p dir="auto">You are right.  And I confirmed that’s been the case in Boost for the older versions used by N++ throughout my tenure on the User Manual… so apparently the section on named capture groups has always had that extra third group that shouldn’t’ve been there.</p>
<p dir="auto">Interestingly, just because the Boost docs doesn’t list it doesn’t mean it’s not invisibly supported by that library: <code>\g'name'</code> or <code>\k'name'</code> as a backreference syntax aren’t mentioned, but it does work.  But in this case, <code>(?(name)...)</code> is neither documented nor supported.</p>
<p dir="auto">This is being fixed in <a href="https://github.com/notepad-plus-plus/npp-usermanual/pull/573" rel="nofollow ugc">PR#573</a></p>
]]></description><link>https://community.notepad-plus-plus.org/post/89502</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/89502</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Mon, 25 Sep 2023 17:25:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to refer named group in RegExp-replacement? on Mon, 25 Sep 2023 16:22:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/3841">@PeterJones</a> I was curious about this and so experimented: The manuals’s <a href="https://npp-user-manual.org/docs/searching/#capture-groups-and-backreferences" rel="nofollow ugc">Capture Groups and Backreferences</a> section documents three ways of defining the names: <code>(?&lt;name&gt;subset)</code> or <code>(?'name'subset)</code> or <code>(?(name)subset)</code> ⇒ Named Capture Group:</p>
<p dir="auto">The first two work for me:<br />
Search: <code>(?&lt;hi&gt;world)</code><br />
Replace: <code>$+{hi}</code><br />
and<br />
Search: <code>(?'hi'world)</code><br />
Replace: <code>$+{hi}</code></p>
<p dir="auto">This fails with <code>Invalid Regular Expression</code> starting at the second <code>(</code><br />
Search: <code>(?(hi)world)</code><br />
Replace: <code>$+{hi}</code></p>
<p dir="auto">The <a href="https://www.boost.org/doc/libs/1_83_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html#boost_regex.syntax.perl_syntax.named_subexpressions" rel="nofollow ugc">Boost manual section in this</a> only mentions the two forms that work.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/89501</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/89501</guid><dc:creator><![CDATA[mkupper]]></dc:creator><pubDate>Mon, 25 Sep 2023 16:22:50 GMT</pubDate></item><item><title><![CDATA[Reply to How to refer named group in RegExp-replacement? on Mon, 25 Sep 2023 16:04:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/29356">@Anton</a> ,</p>
<p dir="auto">As it says in the <a href="https://npp-user-manual.org/docs/searching/#substitutions" rel="nofollow ugc">Searching &gt; Regular Expressions &gt; Substitutions</a> section of the user manual</p>
<table class="table table-bordered table-striped">
<tbody>
<tr>
<td><img src="/assets/uploads/files/1695657483071-dda252d0-bd9c-4cbe-b23d-a5e10c5816cb-image.png" alt="dda252d0-bd9c-4cbe-b23d-a5e10c5816cb-image.png" class=" img-fluid img-markdown" /></td>
</tr>
</tbody>
</table>
<blockquote>
<p dir="auto"><code>\&lt;hi&gt;</code></p>
</blockquote>
<p dir="auto">That is never a valid syntax.  I assume you were trying for the “<a href="https://npp-user-manual.org/docs/searching/#capture-groups-and-backreferences" rel="nofollow ugc">backreference</a>” notation of <code>\g&lt;hi&gt;</code> or <code>\k&lt;hi&gt;</code>.  But as <a href="https://npp-user-manual.org/docs/searching/#capture-groups-and-backreferences" rel="nofollow ugc">the backreference user manual section</a> says,</p>
<table class="table table-bordered table-striped">
<tbody>
<tr>
<td><img src="/assets/uploads/files/1695657853983-4c943e3e-4f86-4100-8710-41c671d1c365-image.png" alt="4c943e3e-4f86-4100-8710-41c671d1c365-image.png" class=" img-fluid img-markdown" /></td>
<td>,</td>
</tr>
</tbody>
</table>
<p dir="auto">so even if you had used the right syntax for a backreference, it would not work in a replacement.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/89500</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/89500</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Mon, 25 Sep 2023 16:04:52 GMT</pubDate></item></channel></rss>