<?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 Macro: Creating Macro to Replace variable text with text determined by text on next line]]></title><description><![CDATA[<p dir="auto">Can anyone help me?</p>
<p dir="auto">I want to create a Macro using Regular Find and Replace to find five consecutive variable characters in a format \D\d\d\d\d and replace them throughout the text with text created from  4 numeric characters \d\d\d\d with text “Census” appended to them.<br />
When I tried to achieve this by recording a Macro, the Macro saved the actual text X382 and 1841Census instead of selecting the text as variables.</p>
<p dir="auto">e.g. In the following text I want to Replace all occurrences of X382 by 1841Census and all occurrences of X391 by 1861Census</p>
<p dir="auto">=== Census===<br />
: 06 JUN 1841.<br />
‘’‘Ridge Town, Bondleigh, Devon, England’‘’.<br />
&lt;ref name=“ref_3”&gt;<br />
Source: &lsqb;&lsqb;#S424&rsqb;&rsqb; Class: HO107; Piece: 250; Book: 10; Civil Parish: Bondleigh; County: Devon; Enumeration District: 12; Folio: 8; Page: 11; Line: 1; GSU roll: 241323.<br />
&lt;/ref&gt;<br />
Note: &lsqb;&lsqb;#X382&rsqb;&rsqb;.<br />
: 07 APR 1861.<br />
‘’‘51, South Street, Tormoham, Devon, England’‘’.<br />
&lt;ref name=“ref_4”&gt;<br />
Source: &lsqb;&lsqb;#S421&rsqb;&rsqb; Class: RG 9; Piece: 1412; Folio: 59; Page: 57; GSU roll: 542809; Enumeration District: 13.<br />
&lt;/ref&gt;<br />
Note: &lsqb;&lsqb;#X391&rsqb;&rsqb;.<br />
.<br />
.</p>
<p dir="auto">.<br />
===&lt;span id=‘X382’&gt;X382&lt;/span&gt;===<br />
1841 England - Census transcript - John COOMBE - Household<br />
Class: HO107; Piece: 250; Book: 10; Civil Parish: Bondleigh; County: Devon; Enumeration District: 12; Folio: 8; Page: 11; Line: 1; GSU roll: 241323.<br />
Ridge Town, Bondleigh, Devon, England</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>AgeM</th>
<th>AgeF</th>
<th>Occupation</th>
<th>BiC</th>
<th>SIF</th>
</tr>
</thead>
<tbody>
<tr>
<td>John COOMBE</td>
<td>60</td>
<td></td>
<td>Farmer</td>
<td>Y</td>
</tr>
<tr>
<td>Joseph COOMBE</td>
<td>28</td>
<td></td>
<td></td>
<td>Y</td>
</tr>
<tr>
<td>Elizabeth COOMBE</td>
<td></td>
<td>22</td>
<td></td>
<td>Y</td>
</tr>
<tr>
<td>George COOMBE</td>
<td>16</td>
<td></td>
<td></td>
<td>Y</td>
</tr>
<tr>
<td>Richard COOMBE</td>
<td>14</td>
<td></td>
<td></td>
<td>Y</td>
</tr>
<tr>
<td>Christopher COOMBE</td>
<td>11</td>
<td></td>
<td></td>
<td>Y</td>
</tr>
<tr>
<td>Francis COOMBE</td>
<td>2</td>
<td></td>
<td></td>
<td>Y</td>
</tr>
</tbody>
</table>
<p dir="auto">===&lt;span id=‘X391’&gt;X391&lt;/span&gt;===<br />
1861 England - Census transcript - Joseph COOMBES - Household<br />
Class: RG 9; Piece: 1412; Folio: 59; Page: 57; GSU roll: 542809; Enumeration District: 13.</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/19059/regex-macro-creating-macro-to-replace-variable-text-with-text-determined-by-text-on-next-line</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 03:48:08 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/19059.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 16 Mar 2020 23:58:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Wed, 18 Mar 2020 22:38:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@Alan-Kilborn</a> Thank you so much, Alan. It’s years since I did any proper programming, though that was a previous occupation. Guess I’m going to have to teach myself to use Python!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51503</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51503</guid><dc:creator><![CDATA[John Slee]]></dc:creator><pubDate>Wed, 18 Mar 2020 22:38:21 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Wed, 18 Mar 2020 18:17:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/195">@guy038</a> said in <a href="/post/51498">Regex Macro: Creating Macro to Replace variable text with text determined by text on next line</a>:</p>
<blockquote>
<p dir="auto">Alan’s python script is more powerful !</p>
</blockquote>
<p dir="auto">Indeed, especially when one notices in the data that some of the references come before the definitions!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51499</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51499</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 18 Mar 2020 18:17:38 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Wed, 18 Mar 2020 18:05:10 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18012">@john-slee</a> and <strong>All</strong>,</p>
<p dir="auto">No problem. Let’s give it a <strong>new</strong> try !</p>
<p dir="auto">From the <strong>initial</strong> lines :</p>
<pre><code class="language-xml">===&lt;span id='X382'&gt;X382&lt;/span&gt;===
1841 England - Census transcript - John COOMBE - Household
</code></pre>
<p dir="auto">The <strong>regex</strong> S/R, described in my <strong>previous</strong> post, changed it as below :</p>
<pre><code class="language-xml">===&lt;span id='1841Census'&gt;X382&lt;/span&gt;===
1841 England - Census transcript - John COOMBE - Household
</code></pre>
<p dir="auto">But, may be, you would <strong>expect</strong> the following result, where the string <strong>X382</strong> is changed, <strong>both</strong>, <strong>inside</strong> the single quotes and <strong>outside</strong> :</p>
<pre><code class="language-xml">===&lt;span id='1841Census'&gt;1841Census&lt;/span&gt;===
1841 England - Census transcript - John COOMBE - Household
</code></pre>
<p dir="auto">If so, use this <strong>new</strong> regex S/R, below :</p>
<p dir="auto">SEARCH <strong><code>(?-si)(===&lt;span id=).+?(?=&lt;/span&gt;.+\R(\d+))</code></strong></p>
<p dir="auto">REPLACE <strong><code>\1'\2Census'&gt;\2Census</code></strong></p>
<p dir="auto">Which can be used, <strong>indifferently</strong>, with the <strong><code>Replace</code></strong> or the <strong><code>Replace All</code></strong> buttons !</p>
<p dir="auto">Cheers,</p>
<p dir="auto">guy038</p>
<p dir="auto"><strong>P.S.</strong> :</p>
<p dir="auto">Of course, the <strong>Alan</strong>’s python script is more <strong>powerful</strong> !</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51498</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51498</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Wed, 18 Mar 2020 18:05:10 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Wed, 18 Mar 2020 17:49:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18012">@John-Slee</a></p>
<p dir="auto">Maybe something like this:</p>
<pre><code>search_repl_pairs_list = []
editor.research(r"(?-s)===&lt;span id='(\D\d\d\d).+\R(\d{4})", lambda m: search_repl_pairs_list.append((m.group(1), m.group(2))))
for tup in search_repl_pairs_list: editor.replace(tup[0], tup[1] + 'Census')
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/51497</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51497</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 18 Mar 2020 17:49:21 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Wed, 18 Mar 2020 17:22:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18012">@John-Slee</a></p>
<p dir="auto">You have more “logic” in your problem statement than a regular expression can handle, I’m afraid.</p>
<p dir="auto">In such cases you should probably turn to a scripting plugin, e.g. Pythonscript, that can work with regular expression data, but can also incorporate more logic into it.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51496</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51496</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 18 Mar 2020 17:22:59 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Wed, 18 Mar 2020 16:01:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/195">@guy038</a> Thanks again. However (again - I hope I’m not pushing my luck by asking once more!) this only replaces the first instance of the string. I need it to replace every occurence of each string in the document.<br />
i.e. In this document, every time X382 occurs it should be replaced by 1841Census and every X391 should be replaced by 1861 Census.<br />
Is this possible?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51493</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51493</guid><dc:creator><![CDATA[John Slee]]></dc:creator><pubDate>Wed, 18 Mar 2020 16:01:41 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Tue, 17 Mar 2020 13:31:58 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18012">@john-slee</a> and <strong>All</strong>,</p>
<p dir="auto">Ah… I’m sorry ! I should have examined your text more <strong>carefully</strong>. No trouble, there’s a <strong>solution</strong>, anyway !</p>
<p dir="auto">If you’ll use the <strong><code>Replace All</code></strong> button, <strong>exclusively</strong>, here is the <strong>right</strong> regex S/R, which may be used, either, in a <strong>macro</strong> :</p>
<p dir="auto">SEARCH <strong><code>(?-si)===&lt;span id='\K.+?(?='.+\R(\d+))</code></strong></p>
<p dir="auto">REPLACE <strong><code>\1Census</code></strong></p>
<p dir="auto"><strong>Notes</strong> :</p>
<ul>
<li>
<p dir="auto">The <strong><code>(?-si)</code></strong> <strong>in-line</strong> modifiers forces the <strong>regex</strong> engine :</p>
<ul>
<li>
<p dir="auto">To consider any <strong>dot</strong> <strong><code>.</code></strong> symbol as matching a <strong>single standard</strong> char, and <strong>not</strong> any <strong>EOL</strong> character</p>
</li>
<li>
<p dir="auto">To process the S/R in a <strong>non-insensitive</strong> way</p>
</li>
</ul>
</li>
<li>
<p dir="auto">Then, the <strong><code>===&lt;span id='</code></strong> matches the <strong>identical</strong> string <strong>===&lt;span id=’</strong></p>
</li>
<li>
<p dir="auto">The special <strong><code>\K</code></strong> syntax <strong>resets</strong> the match process and the regex engine <strong>position</strong></p>
</li>
<li>
<p dir="auto">Therefore, the part <strong><code>.+?</code></strong> matches the <strong>shortest</strong> range of <strong>standard</strong> characters… ( our string <strong>X###</strong> )</p>
</li>
<li>
<p dir="auto">With the <strong>condition</strong>, due to the <strong>look-ahead</strong> structure <strong><code>(?=.........)</code></strong>, that it <strong>must</strong> be followed with :</p>
<ul>
<li>
<p dir="auto">A <strong>single</strong> quote and some <strong>standard</strong> characters <strong><code>'.+</code></strong></p>
</li>
<li>
<p dir="auto">Followed with a <strong>line-break</strong> <strong><code>\R</code></strong> of current line</p>
</li>
<li>
<p dir="auto">Followed with some <strong>digits</strong> characters, stored as <strong>group <code>1</code></strong>, due to the <strong>embedded</strong> parentheses  <strong><code>(\d+)</code></strong></p>
</li>
</ul>
</li>
<li>
<p dir="auto">In the <strong>replacement</strong> <strong><code>\1Census</code></strong>, the <strong>match</strong> ( string <strong>X###</strong> ) is replaced with the <strong>number</strong>, located at <strong>beginning</strong> of <strong>next</strong> line, followed with the string <strong>Census</strong></p>
</li>
</ul>
<hr />
<p dir="auto">Now, if you want to see, at once, the result of each <strong>step by step</strong> replacement, use this <strong>alternate</strong> syntax :</p>
<p dir="auto">SEARCH <strong><code>(?-si)(===&lt;span id=').+?(?='.+\R(\d+))</code></strong></p>
<p dir="auto">REPLACE <strong><code>\1\2Census</code></strong></p>
<p dir="auto"><strong>Notes</strong> :</p>
<ul>
<li>
<p dir="auto">The <strong><code>\K</code></strong> syntax is <strong>not</strong> present. So, the <strong>literal</strong> string <strong>===&lt;span id=’</strong> is <strong>embedded</strong>, itself, in <strong>parentheses</strong> as the <strong>group <code>1</code></strong> <strong><code>(===&lt;span id=')</code></strong> and will be <strong>re-used</strong> in replacement. And, the <strong><code>(\d+)</code></strong> represents the <strong>group <code>2</code></strong></p>
</li>
<li>
<p dir="auto">In <strong>replacement</strong>, it <strong>first</strong> rewrites the beginning of <strong>current</strong> line <strong><code>\1</code></strong>, followed with the <strong>number</strong> <strong><code>\2</code></strong>, at <strong>beginning</strong> of the <strong>2nd</strong> line</p>
</li>
</ul>
<p dir="auto">Cheers,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51460</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51460</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 17 Mar 2020 13:31:58 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Tue, 17 Mar 2020 10:19:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/195">@guy038</a> Thanks for your suggestion. However, I should have made it clearer that the text to be replaced can vary and is not predictable. I therefore need to replace any text between ===&lt;span id’ = and '&gt; and replace it with the relevant text of the form nnnnCensus with nnnn being taken from the first four digits of the following line.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51453</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51453</guid><dc:creator><![CDATA[John Slee]]></dc:creator><pubDate>Tue, 17 Mar 2020 10:19:36 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Tue, 17 Mar 2020 04:08:24 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18012">@john-slee</a> and <strong>All</strong>,</p>
<p dir="auto">You said :</p>
<blockquote>
<p dir="auto">e.g. In the following text I want to Replace all occurrences of X382 by 1841Census and all occurrences of X391 by 1861Census</p>
</blockquote>
<p dir="auto">If so, the correct <strong>regex</strong> syntax which will process <strong>all</strong> values in <strong>one</strong> go, is :</p>
<p dir="auto">SEARCH <strong><code>(?-i)\bX3((82)|91)\b</code></strong></p>
<p dir="auto">REPLACE <strong><code>18(?{2}4:6)1Census</code></strong></p>
<p dir="auto"><strong>Notes</strong> :</p>
<ul>
<li>
<p dir="auto">The part <strong><code>(?-i)</code></strong> forces a <strong>non-insensitive</strong> search</p>
</li>
<li>
<p dir="auto">The part <strong><code>X3</code></strong> looks for the string <strong>X3</strong>, with that <strong>exact</strong> case</p>
</li>
<li>
<p dir="auto">The part <strong><code>((82)|91)</code></strong> means that the string <strong>X3</strong> must be followed with, <strong>either</strong>, the number <strong>82</strong> or the number <strong>91</strong></p>
</li>
<li>
<p dir="auto">The <strong>inner</strong> parentheses represents the <strong>group <code>2</code></strong>. So, if it matches the string <strong>X391</strong>, then <strong>group <code>2</code></strong> is <strong>not</strong> defined</p>
</li>
<li>
<p dir="auto">The <strong>two</strong> assertions <strong><code>\b</code></strong> forces the string <strong>X…</strong> to be surrounded with <strong>non-word</strong> chars for matching</p>
</li>
<li>
<p dir="auto">In <strong>replacement</strong> :</p>
<ul>
<li>
<p dir="auto">It first writes the string <strong>18</strong></p>
</li>
<li>
<p dir="auto">According to <strong>group <code>2</code></strong>,  the part <strong><code>(?{2}4:6)</code></strong> rewrites digit <strong>4</strong> or digit <strong>6</strong></p>
</li>
<li>
<p dir="auto">it finally writes ths string <strong>1Census</strong></p>
</li>
</ul>
</li>
</ul>
<hr />
<p dir="auto">Of course, if you need to run this regex S/R very <strong>often</strong>, it would be sensible to <strong>record</strong> this S/R in a <strong>macro</strong>, and use it with a <strong>shortcut</strong> ;-))</p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51449</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51449</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 17 Mar 2020 04:08:24 GMT</pubDate></item><item><title><![CDATA[Reply to Regex Macro: Creating Macro to Replace variable text with text determined by text on next line on Tue, 17 Mar 2020 00:03:41 GMT]]></title><description><![CDATA[<p dir="auto">I would use the macro twice - once for 1841Census, then again for 1861Census (unless you can create a macro that will replace all occurrences iteratively!)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/51445</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/51445</guid><dc:creator><![CDATA[John Slee]]></dc:creator><pubDate>Tue, 17 Mar 2020 00:03:41 GMT</pubDate></item></channel></rss>