<?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[Regex find and replace string in different types of brackets]]></title><description><![CDATA[<p dir="auto">Hi there!<br />
I have some text. there is some string in it that can be in different types of brackets. For example:</p>
<pre><code>&gt;Some String&lt;
</code></pre>
<p dir="auto">or</p>
<pre><code>"Some string".
</code></pre>
<p dir="auto">I need to replace these strings, with New String, desired result:</p>
<pre><code>&gt;New String&lt;
</code></pre>
<p dir="auto">and</p>
<pre><code>"New String".
</code></pre>
<p dir="auto">I can find and replace <code>&gt;Some String&lt;</code> with <code>&gt;New String&lt;</code> (without any regex), then find and replace “Some String” with “New String” (without any regex).<br />
But if there is some regex expression to do replacement like by one step?<br />
-–</p>
<p dir="auto"><em>moderator added code markdown around text; please don’t forget to <a href="https://community.notepad-plus-plus.org/topic/21925/faq-desk-formatting-forum-posts">use the <code>&lt;/&gt;</code> button to mark example text as “code”</a> so that characters don’t get changed by the forum</em></p>
]]></description><link>https://community.notepad-plus-plus.org/topic/26965/regex-find-and-replace-string-in-different-types-of-brackets</link><generator>RSS for Node</generator><lastBuildDate>Mon, 08 Jun 2026 19:57:40 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/26965.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 22 Jun 2025 21:26:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex find and replace string in different types of brackets on Mon, 23 Jun 2025 14:16:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> said in <a href="/post/102213">Regex find and replace string in different types of brackets</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D0%B5%D0%B2%D0%B3%D0%B5%D0%BD%D0%B8%D0%B9-%D0%BA" aria-label="Profile: Евгений-К">@<bdi>Евгений-К</bdi></a> ,</p>
<p dir="auto">One way to do it simultaneously is</p>
<p dir="auto">FIND = <code>(&gt;Some String&lt;)|("Some String")</code><br />
REPLACE = <code>(?1&gt;New String&lt;)(?2"New String")</code><br />
SEARCH MODE = Regular Expression</p>
<p dir="auto">But that’s “annoying” because you have to repeat the <code>Some String</code> and <code>New String</code> in the FIND and REPLACE.</p>
<p dir="auto">There are some fancy tricks using <a href="https://npp-user-manual.org/docs/searching/#capture-groups-and-backreferences" rel="nofollow ugc">capture groups</a>, either named or numbered, and <a href="https://npp-user-manual.org/docs/searching/#assertions" rel="nofollow ugc">control-flow assertions</a>, to make sure that things balance correctly, and it will only match .</p>
<p dir="auto">FIND = <code>(?'startwrap'(?'angled'&gt;)|(?'quoted'"))\KSome String(?'endwrap'(?=(?('quoted')")(?('angled')&lt;)))</code><br />
REPLACE = <code>New String</code><br />
SEARCH MODE = Regular expression</p>
<p dir="auto">But as you can see, the FIND becomes rather complicated to save the “expense” of having <code>Some String</code> and <code>New String</code> twice.  (You could theoretically do it with numbered capture groups instead, but getting the counts right, especially as you edit to make a third pair like <code>{Some String}</code>, would cause problems, so I used named groups so there was no ambiguity in the future.  I will leave the “numbered group” version as an exercise to the interested reader.)</p>
<p dir="auto">So that’s two ways.  Which is <em>best</em> depends on which you understand, and whether it’s going to be expanded in the future and/or done often.  The most important thing with regex is that you understand what it’s doing, so that you don’t mess up your data.</p>
<p dir="auto">If you don’t understand a “single regex version” but can get it done in two simple search-and-replace that you fully understand, then two simple is probably better for you.  (If it’s something you will be doing often, you can record the two search-and-replace into a macro, so that you can just play that macro in the future.)</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a>, thank you very much for your advice!<br />
I look at the options you suggested and once again I am convinced that RegEx was definitely not created by people and not for people :):):)<br />
Way #1 (captured group) is absolutely clear to me. I myself thought that it was necessary to act somehow through the groups, but I could not understand how.</p>
<p dir="auto">And as you correctly noted - this way in terms of labor costs is not much more efficient than the method without RegEx. The old and new lines have to be written twice.</p>
<p dir="auto">The second way is also clear to me, but only in general terms. This option is a very good warm-up for the mind. And I will definitely save this option for the future.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/102226</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/102226</guid><dc:creator><![CDATA[Евгений К]]></dc:creator><pubDate>Mon, 23 Jun 2025 14:16:52 GMT</pubDate></item><item><title><![CDATA[Reply to Regex find and replace string in different types of brackets on Mon, 23 Jun 2025 10:44:18 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/%D0%B5%D0%B2%D0%B3%D0%B5%D0%BD%D0%B8%D0%B9-%D0%BA" aria-label="Profile: евгений-к">@<bdi>евгений-к</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">Why not this <strong>simple</strong> S/R syntax :</p>
<ul>
<li>
<p dir="auto">FIND <strong><code>(?&lt;=[&gt;"])Some(?= String[&lt;"])</code></strong></p>
</li>
<li>
<p dir="auto">REPLACE <strong><code>New</code></strong></p>
</li>
</ul>
<p dir="auto">Of course, <strong>check</strong> the <strong><code>Regular Expression</code></strong> search mode</p>
<hr />
<p dir="auto">However, if you have <strong>mismatched</strong> delimiters, the solution above would match the expressions <strong><code>&gt;Some String"</code></strong> and <strong><code>"Some String&lt;</code></strong> as well :-((</p>
<p dir="auto">In order to <strong>restrict</strong> the matches to your <strong>two</strong> expressions <strong><code>&gt;Some String&lt;</code></strong> and <strong><code>"Some String".</code></strong>, you could use this <strong>second</strong> S/R :</p>
<ul>
<li>
<p dir="auto">FIND <strong><code>(?&lt;=(&gt;)|")Some(?= String(?(1)&lt;|"))</code></strong></p>
</li>
<li>
<p dir="auto">REPLACE <strong><code>New</code></strong></p>
</li>
</ul>
<pre><code class="language-diff">&gt;Some String&lt;
&gt;Some String"
"Some String".
"Some String&lt;
</code></pre>
<hr />
<p dir="auto"><strong>Notes</strong> :</p>
<ul>
<li>
<p dir="auto">At beginning of the regex, the <strong><code>(?&lt;=(&gt;)|")</code></strong> part is a <strong>look-behind</strong> structure where we <strong>define</strong> the <strong><code>&gt;</code></strong> character as the group <strong><code>1</code></strong></p>
</li>
<li>
<p dir="auto">At the <strong>end</strong> of the search regex, the part <strong><code>(?(1)&lt;|")</code></strong>  is a <strong>conditional</strong> expression, inside the <strong><code>(?= String(?(1)&lt;|"))</code></strong> <strong>look-ahead</strong> structure, which means :</p>
<ul>
<li>
<p dir="auto">Use the <strong><code>&lt;</code></strong> delimiter if the group <strong><code>1</code></strong> is <strong>defined</strong></p>
</li>
<li>
<p dir="auto"><strong>Otherwise</strong>, use the <strong><code>"</code></strong> delimiter</p>
</li>
</ul>
</li>
<li>
<p dir="auto">In the <strong>middle</strong> of the regex, we’re just searching for the <strong><code>Some</code></strong> expression, <em>IF</em> preceded <em>AND</em> followed with the delimiters, that we then replace with the <strong><code>New</code></strong> expression</p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/102222</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/102222</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Mon, 23 Jun 2025 10:44:18 GMT</pubDate></item><item><title><![CDATA[Reply to Regex find and replace string in different types of brackets on Sun, 22 Jun 2025 21:48:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D0%B5%D0%B2%D0%B3%D0%B5%D0%BD%D0%B8%D0%B9-%D0%BA" aria-label="Profile: Евгений-К">@<bdi>Евгений-К</bdi></a> ,</p>
<p dir="auto">One way to do it simultaneously is</p>
<p dir="auto">FIND = <code>(&gt;Some String&lt;)|("Some String")</code><br />
REPLACE = <code>(?1&gt;New String&lt;)(?2"New String")</code><br />
SEARCH MODE = Regular Expression</p>
<p dir="auto">But that’s “annoying” because you have to repeat the <code>Some String</code> and <code>New String</code> in the FIND and REPLACE.</p>
<p dir="auto">There are some fancy tricks using <a href="https://npp-user-manual.org/docs/searching/#capture-groups-and-backreferences" rel="nofollow ugc">capture groups</a>, either named or numbered, and <a href="https://npp-user-manual.org/docs/searching/#assertions" rel="nofollow ugc">control-flow assertions</a>, to make sure that things balance correctly, and it will only match .</p>
<p dir="auto">FIND = <code>(?'startwrap'(?'angled'&gt;)|(?'quoted'"))\KSome String(?'endwrap'(?=(?('quoted')")(?('angled')&lt;)))</code><br />
REPLACE = <code>New String</code><br />
SEARCH MODE = Regular expression</p>
<p dir="auto">But as you can see, the FIND becomes rather complicated to save the “expense” of having <code>Some String</code> and <code>New String</code> twice.  (You could theoretically do it with numbered capture groups instead, but getting the counts right, especially as you edit to make a third pair like <code>{Some String}</code>, would cause problems, so I used named groups so there was no ambiguity in the future.  I will leave the “numbered group” version as an exercise to the interested reader.)</p>
<p dir="auto">So that’s two ways.  Which is <em>best</em> depends on which you understand, and whether it’s going to be expanded in the future and/or done often.  The most important thing with regex is that you understand what it’s doing, so that you don’t mess up your data.</p>
<p dir="auto">If you don’t understand a “single regex version” but can get it done in two simple search-and-replace that you fully understand, then two simple is probably better for you.  (If it’s something you will be doing often, you can record the two search-and-replace into a macro, so that you can just play that macro in the future.)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/102213</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/102213</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Sun, 22 Jun 2025 21:48:16 GMT</pubDate></item></channel></rss>