<?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[Find and Replace: Multiple Replacements in Part of a String]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I’m using the build-in find and replace tool (CTRL+H) with case sensitivity turned on and regular expressions. I’m limited to using vanilla Notepad++ (no plugins, no python etc.).</p>
<p dir="auto">I have a file with sets of ID’s and strings separated by a comma structured like so:</p>
<p dir="auto">ID, String<br />
12345-01, A+A*2B+B*+A<br />
12345-02, A+AB+B*+AA</p>
<p dir="auto">I want to make the following replacements but only in the string (after the comma).<br />
+A*  --&gt; 1<br />
+A --&gt; a<br />
+B* --&gt; 2<br />
+B --&gt; b<br />
2 --&gt; X</p>
<p dir="auto">As example, “12345-01, A+A*2B+B*+A” should be changed to “12345-01, A1XB2a”.</p>
<p dir="auto">Now if the ID was not there the following works like a charm:<br />
<code>Search for: (\+A\*)|(\+A)|(\+B\*)|(\+B)|(2)</code><br />
<code>Replace with: (?{1}1)(?{2}a)(?{3}2)(?{4}b)(?{5}X)</code></p>
<p dir="auto">However, when the ID is present I cannot seem to find a solution that will leave the ID unchanged while making all the replacements in the string.</p>
<p dir="auto">Do you have any suggestions?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/20290/find-and-replace-multiple-replacements-in-part-of-a-string</link><generator>RSS for Node</generator><lastBuildDate>Sun, 14 Jun 2026 09:57:20 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/20290.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 09 Nov 2020 20:42:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Tue, 10 Nov 2020 12:01:47 GMT]]></title><description><![CDATA[<p dir="auto">Hello, @anos, <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</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">And here is my <strong>solution</strong> !</p>
<pre><code class="language-z">If we use the FREE-SPACING mode (?x), for the SEARCH part :

SEARCH  (?x-s)  (?: ( \+A (\*)? ) | ( \+B (\*)? ) | (2) )  (?!.*,)
Groups --&gt;      No  1     2         3     4         5     Look-Ahead  

REPLACE (?1(?{2}1:a))(?3(?{4}2:b))?5X

BEWARE that, in the REPLACE part, the FREE-SPACING mode is FORBIDDEN. So, ONLY for INFO :

REPLACE ( ?1 ( ?{2} 1 : a ) )  ( ?3 ( ?{4} 2 : b ) ) ?5 X
</code></pre>
<p dir="auto">and given the data :</p>
<pre><code class="language-diff">12345-01, A+A*2B+B*+A
12345-02, A+AB+B*+AA
</code></pre>
<p dir="auto">it would return :</p>
<pre><code class="language-diff">12345-01, A1XB2a
12345-02, AaB2aA
</code></pre>
<hr />
<p dir="auto"><strong>Notes</strong> :</p>
<ul>
<li>
<p dir="auto">The first part <strong><code>(?x-s)</code></strong> of the regex <strong>search</strong> means that :</p>
<ul>
<li>
<p dir="auto">The <strong>free-spacing</strong> mode is set ( <strong>Spaces</strong> are <strong>not</strong> taken in account, except for the <strong><code>[ ]</code></strong> syntax or an <strong><code>escaped</code></strong> <strong>space</strong> char )</p>
</li>
<li>
<p dir="auto">Due to <strong><code>(?-s)</code></strong> syntax, the <strong>dot</strong> regex symbol matches a <strong>single standard</strong> char only ( <strong>not</strong> an <strong><code>EOL</code></strong> char )</p>
</li>
</ul>
</li>
<li>
<p dir="auto">Then, the <strong><code>(?:......)</code></strong> syntax defines a <strong>non-capturing</strong> group</p>
</li>
<li>
<p dir="auto">Now, in this <strong>non-capturing</strong> group, we have <strong><code>3</code></strong> <strong>alternatives</strong> and the first <strong>two</strong> contain an <strong>optional inner</strong> group <strong><code>(\*)?</code></strong> ( Remember that the <strong><code>?</code></strong> is an other form of the <strong><code>{0,1}</code></strong> <strong>quantifier</strong> )</p>
</li>
<li>
<p dir="auto">To end, <strong>all</strong> this regex , so far, will match <em>ONLY IF</em>  the final <strong>negative look-ahead</strong> structure <strong><code>(?!.*,)</code></strong> is <strong>verified</strong>, that is to say if at <strong>current</strong> position, reached by the regex engine, there is <em>never</em>  a <strong>comma</strong>, at <strong>any further</strong> position, in <strong>current</strong> line</p>
</li>
<li>
<p dir="auto">Now, in the <strong>replacement</strong> regex :</p>
<ul>
<li>
<p dir="auto">The  <strong><code>(?1(?{2}1:a))</code></strong> syntax means that <em>if</em>  <strong>group</strong> <strong><code>1</code></strong> exists, <em>then if</em>  <strong>group</strong> <strong><code>2</code></strong> exists, <em>then</em>  write <strong><code>1</code></strong> <em>else</em>  write <strong><code>a</code></strong></p>
</li>
<li>
<p dir="auto">The  <strong><code>(?3(?{4}2:b))</code></strong> syntax means that <em>if</em>  <strong>group</strong> <strong><code>3</code></strong> exists, <em>then if</em>  <strong>group</strong> <strong><code>4</code></strong> exists, <em>then</em>  write <strong><code>2</code></strong> <em>else</em>  write <strong><code>b</code></strong></p>
</li>
<li>
<p dir="auto">Finally, the <strong><code>?5X</code></strong> means that <em>if</em>  <strong>group</strong> <strong><code>5</code></strong> exists, <em>then</em>  write an <strong><code>X</code></strong> ( The <strong>parentheses</strong> are <strong>not</strong> mandatory as this part <strong>ends</strong> the regex</p>
</li>
<li>
<p dir="auto">Note also that it’s <strong>not</strong> necessary to surround the <strong>groups</strong> <strong><code>1</code></strong>, <strong><code>3</code></strong> and <strong><code>5</code></strong> with <strong>braces</strong> as these groups are <strong>not</strong> immediately followed with a <strong>digit</strong> !</p>
</li>
</ul>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59597</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59597</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 10 Nov 2020 12:01:47 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 21:24:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> Thank you for this solution. This also gets the job done, and as <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> points out it seems to be more efficient.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59596</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59596</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 09 Nov 2020 21:24:36 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 21:21:20 GMT]]></title><description><![CDATA[<p dir="auto">@Anos said in <a href="/post/59594">Find and Replace: Multiple Replacements in Part of a String</a>:</p>
<blockquote>
<p dir="auto">I have never really familiarized myself with lookaheads</p>
</blockquote>
<p dir="auto">There are LOTS of wonderful things to try and remember, as <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> just reminded me. I should have made that a non-capture group, then it would not have required a rejig of the replace with code.</p>
<p dir="auto">As I always say<br />
“The day you stop learning is the day you die”</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59595</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59595</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 09 Nov 2020 21:21:20 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 21:18:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> This does seem to work as intended, at least with my limited testing. Thank you very much for your quick replies. I have never really familiarized myself with lookaheads, they certainly look useful though.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59594</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59594</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 09 Nov 2020 21:18:30 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 21:17:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> said in <a href="/post/59591">Find and Replace: Multiple Replacements in Part of a String</a>:</p>
<blockquote>
<p dir="auto">find: (^[^,]+,)|(+A*)|(+A)|(+B*)|(+B)|(2)<br />
repl: (?{1}\1)(?{2}1)(?{3}a)(?{4}2)(?{5}b)(?{6}X)</p>
</blockquote>
<p dir="auto">I vote for yours. As an interesting aside, using <a href="http://regex101.com" rel="nofollow ugc">regex101.com</a> and inputting the 2 example lines and the Find What code, my code took twice as long as <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> to process. It’s obvious the lookahead is where the extra time is spent.</p>
<p dir="auto">For a small file to process it may not mean a lot, but sometimes efficiency in coding can be an advantage, hence my vote for <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> code.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59593</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59593</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 09 Nov 2020 21:17:57 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 21:06:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> said in <a href="/post/59590">Find and Replace: Multiple Replacements in Part of a String</a>:</p>
<blockquote>
<p dir="auto">So as I had to add a negative lookahead the bracket numbering all changed</p>
</blockquote>
<p dir="auto">You could have made the wrapping parentheses a non-capturing group: <code>(?-s)(?:(\+A\*)|(\+A)|(\+B\*)|(\+B)|(2))(?!.*?,)</code>, to avoid the renumbering in the replacement.</p>
<p dir="auto">TIMTOWTDI</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59592</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59592</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Mon, 09 Nov 2020 21:06:35 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 21:05:54 GMT]]></title><description><![CDATA[<p dir="auto">@Anos</p>
<p dir="auto">I came up with this; seems to work but maybe has holes:</p>
<p dir="auto">find: <code>(^[^,]+,)|(\+A\*)|(\+A)|(\+B\*)|(\+B)|(2)</code><br />
repl: <code>(?{1}\1)(?{2}1)(?{3}a)(?{4}2)(?{5}b)(?{6}X)</code></p>
<p dir="auto">The result of the replacement with it:</p>
<pre><code class="language-z">12345-01, A1XB2a
12345-02, AaB2aA
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/59591</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59591</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Mon, 09 Nov 2020 21:05:54 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 21:02:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> said in <a href="/post/59589">Find and Replace: Multiple Replacements in Part of a String</a>:</p>
<blockquote>
<p dir="auto">It will involve a bit more thought.</p>
</blockquote>
<p dir="auto">Sorry, about that false start, I think I now have it. We have<br />
FW:<code>(?-s)((\+A\*)|(\+A)|(\+B\*)|(\+B)|(2))(?!.*?,)</code><br />
RW:<code>(?{2}1)(?{3}a)(?{4}2)(?{5}b)(?{6}X)</code></p>
<p dir="auto">So as I had to add a negative lookahead the bracket numbering all changed hence a new replace with code as well.<br />
So basically whenever it finds a character, so long as no <code>,</code> after it on the line it will be changed. As the ID is before the <code>,</code> nothing there should be changed.</p>
<p dir="auto">Terry</p>
<p dir="auto">PS should have paid more attention to your statement<br />
I want to make the following replacements but only in the string (after the comma).</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59590</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59590</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 09 Nov 2020 21:02:53 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 20:56:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> said in <a href="/post/59588">Find and Replace: Multiple Replacements in Part of a String</a>:</p>
<blockquote>
<p dir="auto">So here the 2 must be at a "boundary</p>
</blockquote>
<p dir="auto">Sorry, jumped the gun slightly, it did work on 2nd example, missed that it didn’t work on the first examples. Yes it IS a bit of a poser. It will involve a bit more thought.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59589</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59589</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 09 Nov 2020 20:56:01 GMT</pubDate></item><item><title><![CDATA[Reply to Find and Replace: Multiple Replacements in Part of a String on Mon, 09 Nov 2020 20:49:59 GMT]]></title><description><![CDATA[<p dir="auto">@Anos said in <a href="/post/59587">Find and Replace: Multiple Replacements in Part of a String</a>:</p>
<blockquote>
<p dir="auto">that will leave the ID unchanged</p>
</blockquote>
<p dir="auto">A quick “fix” might be to use:<br />
<code>(\+A\*)|(\+A)|(\+B\*)|(\+B)|(\b2\b)</code><br />
So here the 2 must be at a "boundary. For the 2 example lines provided it does work, however 2 examples does NOT a book make! It will depend on whether the “2” in the rest of the expression is surrounded by different characters on both sides.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/59588</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/59588</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 09 Nov 2020 20:49:59 GMT</pubDate></item></channel></rss>