<?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[Is it possible to replace individual characters inside a regex capture group?]]></title><description><![CDATA[<p dir="auto">I’ve searched the forum for other solutions, but they don’t seem to answer the question I have in mind.</p>
<p dir="auto">I want to add <code>id</code> attributes to all HTML header elements (e.g., <code>h1</code>, <code>h2</code>, <code>h3</code>, etc) using the content of each element as the basis for the <code>id</code> attribute, by making them lowercase and replacing spaces with hyphens.</p>
<p dir="auto">For example, I simply want to do a replacement from this:</p>
<pre><code>&lt;h1&gt;Primary Article&lt;/h1&gt;
&lt;h2&gt;Subtopic&lt;/h2&gt;
&lt;h1&gt;A Different Approach to All This&lt;/h1&gt;
</code></pre>
<p dir="auto">to this:</p>
<pre><code>&lt;h1 id="primary-article"&gt;Primary Article&lt;/h1&gt;
&lt;h2 id="subtopic"&gt;Subtopic&lt;/h2&gt;
&lt;h1 id="a-different-approach-to-all-this"&gt;A Different Approach to All This&lt;/h1&gt;
</code></pre>
<p dir="auto">This it the farthest I’ve gotten:</p>
<p dir="auto">Search: <code>&lt;h(\d)&gt;(.*?)&lt;/h\d&gt;</code><br />
Replace: <code>&lt;h\1 id="\L\2\E"&gt;\2&lt;/h\1&gt;</code></p>
<p dir="auto">It does everything I need, except replace the spaces in the capture group with dashes. <strong>Is this even possible?</strong></p>
<p dir="auto">If not, how would you approach this problem?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/24263/is-it-possible-to-replace-individual-characters-inside-a-regex-capture-group</link><generator>RSS for Node</generator><lastBuildDate>Mon, 15 Jun 2026 01:29:34 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/24263.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Mar 2023 18:55:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Is it possible to replace individual characters inside a regex capture group? on Mon, 20 Mar 2023 20:33:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pbarney" aria-label="Profile: pbarney">@<bdi>pbarney</bdi></a> said in <a href="/post/85039">Is it possible to replace individual characters inside a regex capture group?</a>:</p>
<blockquote>
<p dir="auto">If I owned a software development business, I’d hire you as Chief RegEx Officer.</p>
</blockquote>
<p dir="auto">I fully endorse this.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/85040</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/85040</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Mon, 20 Mar 2023 20:33:54 GMT</pubDate></item><item><title><![CDATA[Reply to Is it possible to replace individual characters inside a regex capture group? on Mon, 20 Mar 2023 20:21:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a>, Thank you for that masterful example. I’ve seen a few of your posts before, and while the regex is mostly beyond me, I appreciate you offering workable examples.</p>
<p dir="auto">If I owned a software development business, I’d hire you as Chief RegEx Officer.</p>
<p dir="auto">While my constraints were small, future searchers who end up here might be interested in a regex that could handle <code>&lt;h?&gt;</code> elements that already have existing attributes…</p>
<p dir="auto">For example:<br />
<code>&lt;h1 class="article"&gt;Top Level&lt;/h1&gt;</code><br />
yielding:<br />
<code>&lt;h1 id="top-level" class="article"&gt;Top Level&lt;/h1&gt;</code>.</p>
<p dir="auto">Would it be difficult to add that to your suggested offerings?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/85039</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/85039</guid><dc:creator><![CDATA[pbarney]]></dc:creator><pubDate>Mon, 20 Mar 2023 20:21:17 GMT</pubDate></item><item><title><![CDATA[Reply to Is it possible to replace individual characters inside a regex capture group? on Sat, 18 Mar 2023 00:15:14 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/pbarney" aria-label="Profile: pbarney">@<bdi>pbarney</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">I agree with <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a> <strong>statement</strong> when he said :</p>
<blockquote>
<p dir="auto">Often the easy way is to do the task in several steps</p>
</blockquote>
<p dir="auto">Moreover, there is an <strong>objective</strong> reason why this <strong>cannot</strong> be done in one go ! Indeed, you have to <strong>globally</strong> add all <strong><code>id</code></strong> attributes before, <strong>individually</strong>, replace any <strong><code>space</code></strong> char with a <strong><code>dash</code></strong> character, in the <strong><code>id</code></strong> regions !</p>
<hr />
<p dir="auto">So, given your <em>INPUT</em> text :</p>
<pre><code class="language-diff">&lt;h1&gt;Primary Article&lt;/h1&gt;
&lt;h2&gt;Subtopic&lt;/h2&gt;
&lt;h1&gt;A Different Approach to All This&lt;/h1&gt;
</code></pre>
<p dir="auto">With this <strong>first</strong> regex S/R, we’ll <strong>add</strong> the <strong><code>id</code></strong> attributes, from the <strong>contents</strong> of each element, to any <strong><code>h#</code></strong> <strong>header</strong> :</p>
<p dir="auto">SEARCH <strong><code>(?x-si) ( ^ \x20* &lt; h[1-6] ) (?= &gt; ( .+? ) &lt; )</code></strong>    OR    <strong><code>(?-si)(^\x20*&lt;h[1-6])(?=&gt;(.+?)&lt;)</code></strong></p>
<p dir="auto">REPLACE <strong><code>\1 id="\L\2"</code></strong></p>
<p dir="auto">And you get this <strong>temporary</strong> text :</p>
<pre><code class="language-diff">&lt;h1 id="primary article"&gt;Primary Article&lt;/h1&gt;
&lt;h2 id="subtopic"&gt;Subtopic&lt;/h2&gt;
&lt;h1 id="a different approach to all this"&gt;A Different Approach to All This&lt;/h1&gt;
</code></pre>
<hr />
<p dir="auto">Then, use this <strong>second</strong> regex S/R to replace any <strong><code>space</code></strong> char, in the <strong><code>id</code></strong> region, with a <strong><code>dash</code></strong> character :</p>
<p dir="auto">SEARCH <strong><code>(?x-si) (?: ^ \x20* &lt; h[1-6] \x20 | (?! \A ) \G ) (?: (?! &gt; ) . )*? \K \x20</code></strong>    OR    <strong><code>(?-si)(?:^\x20*&lt;h[1-6]\x20|(?!\A)\G)(?:(?!&gt;).)*?\K\x20</code></strong></p>
<p dir="auto">REPLACE <strong><code>-</code></strong></p>
<p dir="auto">And here is your <strong>expected</strong> <em>OUTPUT</em> text :</p>
<pre><code class="language-diff">&lt;h1 id="primary-article"&gt;Primary Article&lt;/h1&gt;
&lt;h2 id="subtopic"&gt;Subtopic&lt;/h2&gt;
&lt;h1 id="a-different-approach-to-all-this"&gt;A Different Approach to All This&lt;/h1&gt;
</code></pre>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84943</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84943</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Sat, 18 Mar 2023 00:15:14 GMT</pubDate></item><item><title><![CDATA[Reply to Is it possible to replace individual characters inside a regex capture group? on Fri, 17 Mar 2023 20:28:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pbarney" aria-label="Profile: pbarney">@<bdi>pbarney</bdi></a><br />
I’m not actually on a PC to do any testing, however I actually can’t think of a way to do it. Even if it can be done the regex would be horribly complicated and practically unsupportable if changes were required later on.</p>
<p dir="auto">Often the easy way is to do the task in several steps.</p>
<p dir="auto">Terry</p>
<p dir="auto">PS also consider creating a macro which can run several steps so in the end you are just running a single process. Details are referenced in the online manual and lots of posts on this forum.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84936</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84936</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Fri, 17 Mar 2023 20:28:03 GMT</pubDate></item><item><title><![CDATA[Reply to Is it possible to replace individual characters inside a regex capture group? on Fri, 17 Mar 2023 19:46:29 GMT]]></title><description><![CDATA[<p dir="auto">Thank you, <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a>. I’ll take a look at them now.</p>
<p dir="auto">For future searchers, here are links to the two posts Terry-R mentioned:</p>
<ul>
<li><a href="https://community.notepad-plus-plus.org/post/74899">Generic Regular Expression (regex) Formulas</a></li>
<li><a href="https://community.notepad-plus-plus.org/post/75007">Replacing in a specific zone of text</a></li>
</ul>
<p dir="auto">May I ask why you wouldn’t attempt to combine them?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84935</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84935</guid><dc:creator><![CDATA[pbarney]]></dc:creator><pubDate>Fri, 17 Mar 2023 19:46:29 GMT</pubDate></item><item><title><![CDATA[Reply to Is it possible to replace individual characters inside a regex capture group? on Fri, 17 Mar 2023 19:33:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pbarney" aria-label="Profile: pbarney">@<bdi>pbarney</bdi></a> said in <a href="/post/84929">Is it possible to replace individual characters inside a regex capture group?</a>:</p>
<blockquote>
<p dir="auto">It does everything I need, except replace the spaces in the capture group with dashes. Is this even possible?</p>
</blockquote>
<p dir="auto">It certainly is possible, just read the FAQ post “Generic Regular Expression (regex) Formulas” and the linked post “Replacing in a specific zone of text”.</p>
<p dir="auto">It is best to keep these 2 tasks separate, I would  not even attempt to combine the 2 edits.</p>
<p dir="auto">Tery</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84933</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84933</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Fri, 17 Mar 2023 19:33:19 GMT</pubDate></item></channel></rss>