<?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: Select only the first instance of search results &#x2F; first match]]></title><description><![CDATA[<p dir="auto">hello. I have a html bug. And I have to change it in more then 2000 files.</p>
<p dir="auto">I need to replace only the first instance of a search result.</p>
<p dir="auto">For example, I have this line  <strong>&lt;div class=“pagination”&gt;</strong> . The problem is that this line is repeated 5 times in each html file. So, if I want to search and replace this line, I risk to delete all other same lines.</p>
<p dir="auto">My search should stop of first match.</p>
<p dir="auto">I made a little regex, but does’t work for me.</p>
<p dir="auto"><code>^.*(&lt;div class="pagination"&gt;)((?s:)).*/</code></p>
<p dir="auto">Can anyone help me, please?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/12353/regex-select-only-the-first-instance-of-search-results-first-match</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 16:23:23 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/12353.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 11 Sep 2016 13:40:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Sun, 17 Nov 2024 13:42:12 GMT]]></title><description><![CDATA[<p dir="auto">On testing the above, I observed that both the above regular expressions work only for tags or strings that begin with a <code>&lt;</code> and end with a <code>&gt;</code> - so if you are searching for a string between inverted commas, to find the first string, you should use the regular expression <code>(?s)\A.*?\K"string(?:.*?)?"</code></p>
]]></description><link>https://community.notepad-plus-plus.org/post/97949</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/97949</guid><dc:creator><![CDATA[dr ramaanand]]></dc:creator><pubDate>Sun, 17 Nov 2024 13:42:12 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Sun, 17 Nov 2024 10:59:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: Vasile-Caraus">@<bdi>Vasile-Caraus</bdi></a> The regular expression <code>(?s)\A.*?\Kstring(?:.*?)?&gt;</code> helps find the very first occurrence of a string and if you want to find the first occurrence of a tag, say TAG_2, AFTER the first occurrence of another tag, say TAG_1, my generic regex becomes :</p>
<p dir="auto"><code>(?s-i)\A.*?&lt;TAG_1(?: .*?)?&gt;.*?\K&lt;TAG_2(?: .*?)?&gt;</code> as per <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a></p>
]]></description><link>https://community.notepad-plus-plus.org/post/97947</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/97947</guid><dc:creator><![CDATA[dr ramaanand]]></dc:creator><pubDate>Sun, 17 Nov 2024 10:59:01 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Fri, 19 Feb 2021 15:31:20 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> thanks a lot !</p>
]]></description><link>https://community.notepad-plus-plus.org/post/63058</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/63058</guid><dc:creator><![CDATA[Vasile Caraus]]></dc:creator><pubDate>Fri, 19 Feb 2021 15:31:20 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Fri, 19 Feb 2021 00:07:04 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> said in <a href="/post/63042">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto">But, unless I’m mistaken, doesn’t this regex, below, do the same search ?<br />
(?s)\A.*\KRE</p>
</blockquote>
<p dir="auto">Yes, indeed.<br />
That’s what I get for dabbling in the area of another master! :-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/63044</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/63044</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Fri, 19 Feb 2021 00:07:04 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Thu, 18 Feb 2021 23:24:33 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> said in <a href="/post/63042">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: vasile-caraus">@<bdi>vasile-caraus</bdi></a>, <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 All,<br />
My God !! Of course, the <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a>’s regex is just magic and so simple !</p>
</blockquote>
<p dir="auto">I feel like I’m being rewarded for something I <s>stole</s> borrowed now. ;-)) All I did was point out the marvellous creation of <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> and how by the absence of a single character it turns one thing into another.</p>
<p dir="auto">But hey, I’m happy that collectively we can show there are many answers, all work in various ways.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/63043</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/63043</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Thu, 18 Feb 2021 23:24:33 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Thu, 18 Feb 2021 23:19:04 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: vasile-caraus">@<bdi>vasile-caraus</bdi></a>, <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">My God !! Of course, the <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a>’s regex is just <strong>magic</strong> and so <strong>simple</strong> ! Congratulations, <strong>Terry</strong> ;-)) How could we <strong>not</strong> think of it ??</p>
<p dir="auto">If I adapt <strong>Terry</strong> concept to the regexes of my <strong>previous</strong> post, everything becomes <strong>crystal clear</strong> :</p>
<p dir="auto">SEARCH <strong><code>(?s-i)\A.*?&lt;tr&gt;\K.*?(?=&lt;/tr&gt;)</code></strong> to search ( and replace ) the <strong>first</strong> <strong><code>&lt;tr&gt; ••••• &lt;/tr&gt;</code></strong> block</p>
<p dir="auto">SEARCH <strong><code>(?s-i)\A.*&lt;tr&gt;\K.*?(?=&lt;/tr&gt;)</code></strong> to search ( and replace ) the  <strong>last</strong> <strong><code>&lt;tr&gt; ••••• &lt;/tr&gt;</code></strong> block</p>
<p dir="auto">As usual, tick the <strong><code>Regular expression</code></strong> and <strong><code>Wrap around</code></strong> options and click on the <strong><code>Replace All</code></strong> button, <strong>exclusively</strong></p>
<hr />
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: vasile-caraus">@<bdi>vasile-caraus</bdi></a>, this demonstrates, in a <strong>masterful</strong> way, that things can be <strong>skillfully</strong> solved by <strong>other</strong> people than me and moreover… by <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a> !!</p>
<hr />
<p dir="auto">Now, <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> you said :</p>
<blockquote>
<p dir="auto">Match the last occurrence of a regular expression <strong>RE</strong>:</p>
</blockquote>
<blockquote>
<p dir="auto"><strong><code>(?s)\A.*(</code>RE<code>).*?\K\1</code></strong></p>
</blockquote>
<p dir="auto">But, unless I’m mistaken, doesn’t this <strong>regex</strong>, below, do the <strong>same</strong> search ?</p>
<p dir="auto"><strong><code>(?s)\A.*\K</code>RE</strong></p>
<p dir="auto">Best regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/63042</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/63042</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Thu, 18 Feb 2021 23:19:04 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Thu, 18 Feb 2021 23:24:25 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: vasile-caraus">@<bdi>vasile-caraus</bdi></a>, <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"><strong>IMPORTANT</strong> : I wrote this post, <strong>after</strong> reading posts from the banner <em>4 YEARS LATER</em> till the <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a>’s post, below :</p>
<p dir="auto"><a href="https://community.notepad-plus-plus.org/post/62964">https://community.notepad-plus-plus.org/post/62964</a></p>
<p dir="auto">But I going to add a <strong>second</strong> post, after reading the <strong>last recent</strong> solutions ! Sorry for my <strong>incomplete</strong> work !</p>
<hr />
<p dir="auto">First, <a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: vasile-caraus">@<bdi>vasile-caraus</bdi></a>, I <strong>totally</strong> agree to @alan-kilbron’s <strong>comment</strong> on your attitude ! Not very <strong>fair</strong> and <strong>nice</strong> to <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-r">@<bdi>Terry-r</bdi></a>, which was trying to help you :-((</p>
<p dir="auto">Seemingly, you quite know, by now, the <strong>powerful</strong> of regexes, regarding text <strong>manipulations</strong>. And if you had studied, <strong>seriously</strong>, some regex tutorials, you would <strong>not</strong> have spoken about that regex <strong><code>(?s)\z.*?&lt;tr&gt;\s*\K.*?(\s*&lt;/tr&gt;)</code></strong> which is a complete <strong>nonsense</strong> !</p>
<p dir="auto">For instance, from the <strong>two</strong> pages of the <strong><code>Regular-expressions.info</code></strong> site, below, you had understood, <strong>at once</strong>, that the <strong><code>\z</code></strong> syntax <strong>always</strong> comes at the <strong>very end</strong> of a regex expression or, possibly, <strong>before</strong> an alternation symbol <strong><code>|</code></strong> !!</p>
<p dir="auto"><a href="https://www.regular-expressions.info/anchors.html" rel="nofollow ugc">https://www.regular-expressions.info/anchors.html</a></p>
<p dir="auto"><a href="https://www.regular-expressions.info/refanchors.html" rel="nofollow ugc">https://www.regular-expressions.info/refanchors.html</a></p>
<hr />
<p dir="auto">Now, I <strong>slightly</strong> simplified the <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a>’s search regex, which searches for the <strong>first</strong> element <strong><code>&lt;tr&gt; ••••• &lt;/tr&gt;</code></strong>, of an <strong><code>HTML</code></strong> page :</p>
<p dir="auto">SEARCH <strong><code>(?s-i)\A.*?&lt;tr&gt;\K.*?(?=&lt;/tr&gt;)</code></strong></p>
<p dir="auto">In return, if your <strong>replacement</strong> regex is :</p>
<ul>
<li>The expression <strong><code>Here is the NEW text</code></strong>, you’ll get the simple text</li>
</ul>
<pre><code class="language-html"> &lt;/tr&gt;Here is the NEW text&lt;/tr&gt;
</code></pre>
<ul>
<li>The expression is <strong><code>\r\nHere is the NEW text\r\n</code></strong> the <strong>output</strong> text will be :</li>
</ul>
<pre><code class="language-html">&lt;tr&gt;
Here is the NEW text
&lt;/tr&gt;
</code></pre>
<ul>
<li>
<p dir="auto">Tick the <strong><code>Wrap around</code></strong> option</p>
</li>
<li>
<p dir="auto">Click on  the <strong><code>Replace All</code></strong> button, <strong>exclusively</strong> !</p>
</li>
</ul>
<hr />
<p dir="auto">Now, to search for the <strong>last</strong> element <strong><code>&lt;tr&gt; ••••• &lt;/tr&gt;</code></strong>, of an <strong><code>HTML</code></strong> page, use the following <strong>regex</strong> :</p>
<p dir="auto">SEARCH <strong><code>(?s-i)&lt;tr&gt;\K((?!&lt;tr&gt;).)*?(?=&lt;/tr&gt;((?!&lt;tr&gt;).)*?\z)</code></strong></p>
<p dir="auto">Note that I use exactly the <strong>scheme</strong> proposed by <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: Peterjones">@<bdi>Peterjones</bdi></a> :</p>
<pre><code class="language-z">
- find from &lt;tr&gt; to &lt;/tr&gt; ( NOT included )          =&gt;    (?s-i)&lt;tr&gt;\K •••••••••• (?=&lt;/tr&gt; •••••••••• )
                                                                           ^                 ^    ^
                                                                           |                 |    |
- WITHOUT any contained &lt;tr&gt;                        =&gt;    ((?!&lt;tr&gt;).)*? ---•                 |    |
																							 |    |
- FOLLOWED by anything that’s NOT a &lt;tr&gt;            =&gt;    ((?!&lt;tr&gt;).)*? ---------------------•    |
																								  |
- until the VERY END of the file                    =&gt;    \z -------------------------------------•
</code></pre>
<hr />
<p dir="auto">To <strong>All</strong> :</p>
<p dir="auto">You could ask me : <strong>why</strong> the regex to search for the <strong>last</strong> <strong><code>&lt;tr&gt; ••••• &lt;/tr&gt;</code></strong> block is <strong>more</strong> complicated than the one to search for the <strong>first</strong> one ?</p>
<p dir="auto">This is because of the general <strong>direction</strong> used by the <strong>regex engine</strong> : from <em>LEFT</em> to <em>RIGHT</em> !</p>
<ul>
<li>
<p dir="auto">Indeed, when we search for <strong><code>(?s-i)\A.*?&lt;tr&gt;</code></strong>, part of the <strong>first</strong> regex, the range of any char <strong><code>(?s).*</code></strong> with the <strong>lazy</strong> quantifier <strong><code>?</code></strong> is then <strong>extended</strong> to the <strong>first</strong> occurrence of the string <strong><code>&lt;tr&gt;</code></strong> and means that, necessarily, this range <strong>cannot</strong> contain any <strong><code>&lt;tr&gt;</code></strong> inside !</p>
</li>
<li>
<p dir="auto">Similarly, the regex  <strong><code>(?s).*?(?=&lt;/tr&gt;)</code></strong> would search for any range of any char, possibly empty, till the <strong>nearest</strong> string <strong><code>&lt;/tr&gt;</code></strong>, meaning, <strong>implicitly</strong>, that this range of chars cannot contain a <strong><code>&lt;/tr&gt;</code></strong> string</p>
</li>
<li>
<p dir="auto">Whereas, when searching the last <strong><code>&lt;tr&gt; ••••• &lt;/tr&gt;</code></strong> block, as our <strong>reference</strong> is the anchor <strong><code>\z</code></strong> ( <strong>very end</strong> of current file ), we must build up the regex, using a kind of <strong>back-propagation</strong> method :</p>
<ul>
<li>
<p dir="auto">Starting from the <strong>very end</strong> of file</p>
</li>
<li>
<p dir="auto">Moving <strong>back</strong>, through characters <strong>without</strong> any <strong><code>&lt;tr&gt;</code></strong> string</p>
</li>
<li>
<p dir="auto">Till a <strong><code>&lt;/tr&gt;</code></strong> string</p>
</li>
<li>
<p dir="auto">Moving <strong>back</strong>, again, through characters <strong>without</strong> any <strong><code>&lt;tr&gt;</code></strong> string</p>
</li>
<li>
<p dir="auto">Till a <strong><code>&lt;tr&gt;</code></strong> string</p>
</li>
</ul>
</li>
</ul>
<p dir="auto">Of course, I assume that any <strong><code>&lt;tr&gt;</code></strong> <strong>correctly</strong> ends with <strong><code>&lt;/tr&gt;</code></strong> !</p>
<p dir="auto">Test these <strong>two</strong> regexes against this sample, derived from <strong>Peter</strong>’s one, which contains <strong><code>4</code></strong> blocks <strong><code>&lt;/tr&gt; •••• &lt;/tr&gt;</code></strong> :</p>
<pre><code class="language-html">&lt;html&gt;&lt;body&gt;
&lt;table&gt;
&lt;tr&gt;
get rid of stuff, in case of \A anchor, including &lt;embedded/&gt; &lt;tags/&gt;
&lt;/tr&gt;
&lt;tr&gt;
keep stuff including &lt;embedded/&gt; &lt;tags/&gt;
&lt;/tr&gt;
&lt;tr&gt;
keep stuff including &lt;embedded/&gt; &lt;tags/&gt;
&lt;/tr&gt;
&lt;tr&gt;
get rid of stuff, in case of \z anchor, including &lt;embedded/&gt; &lt;tags/&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p dir="auto">The <strong>first</strong> regex, with the <strong><code>\A</code></strong> syntax should replace the <strong>first</strong> block, only and the <strong>last</strong> regex, with the <strong><code>\z</code></strong> syntax, should replace the <strong>fourth</strong> and <strong>last</strong> <strong><code>&lt;tr&gt;</code></strong> block</p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
<p dir="auto"><strong>P.S.</strong> :</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: vasile-caraus">@<bdi>vasile-caraus</bdi></a>, note that I’m <strong>willing</strong>, and probably, all people involved in that <strong>discussion</strong>, to help you if you have difficulty understanding a <strong>specific</strong> part of a regex <strong>tutorial</strong>, that you have decided to study. A <strong>different</strong> perspective will certainly be very <strong>useful</strong> to you … and others ;-))</p>
]]></description><link>https://community.notepad-plus-plus.org/post/63041</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/63041</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Thu, 18 Feb 2021 23:24:25 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Thu, 18 Feb 2021 01:49:23 GMT]]></title><description><![CDATA[<p dir="auto">This all seems rather “special case”.<br />
This <code>&lt;tr&gt;</code> and <code>&lt;/tr&gt;</code> junk…</p>
<p dir="auto">To be generic, that is, a roadmap for other interested parties to use, why not specify it like this:</p>
<hr />
<p dir="auto">Match only the first occurrence in a file of a regular expression <strong>RE</strong>:</p>
<p dir="auto"><code>(?s)\A.*?\K</code><strong>RE</strong></p>
<hr />
<p dir="auto">Match the last occurrence of a regular expression <strong>RE</strong>:</p>
<p dir="auto"><code>(?s)\A.*(</code><strong>RE</strong><code>).*?\K\1</code></p>
<hr />
<p dir="auto">Of course, clearly the <strong>RE</strong> has to be something a bit more specific than (example) <code>..</code>, but these seem to mostly work to achieve the goal.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/63023</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/63023</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Thu, 18 Feb 2021 01:49:23 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 11:57:36 GMT]]></title><description><![CDATA[<p dir="auto">so, conclusion. I select all regex from the las converstion:</p>
<p dir="auto"><strong>Select and replace the first instance:</strong></p>
<p dir="auto">SEARCH: <code>(?s)\A.*?&lt;tr&gt;\s*\K.*?(\s*&lt;/tr&gt;)(?=$)</code><br />
REPLACE BY: <code>NEW CONTENT $1</code></p>
<p dir="auto"><strong>or</strong></p>
<p dir="auto">SEARCH: <code>(?s)\A.*?&lt;tr&gt;\s*\K.*?(\s*&lt;/tr&gt;)</code><br />
REPLACE BY: <code>NEW CONTENT $1</code></p>
<p dir="auto"><strong>Select and replace the last instance:</strong></p>
<p dir="auto">SEARCH: <code>(?s)&lt;tr&gt;.*&lt;/tr&gt;.*?&lt;tr&gt;\K.+?(?=&lt;/tr&gt;.*?\z)</code><br />
REPLACE BY: <code>\r NEW CONTENS $1 \r</code></p>
<p dir="auto"><strong>or</strong></p>
<p dir="auto">SEARCH: <code>(?s)\A.*&lt;tr&gt;\K.+?(?=&lt;/tr&gt;)</code><br />
REPLACE BY: <code>\r NEW CONTENS $1 \r</code></p>
<p dir="auto">WORKS. Thanks a lot friends.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62986</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62986</guid><dc:creator><![CDATA[Vasile Caraus]]></dc:creator><pubDate>Wed, 17 Feb 2021 11:57:36 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 02:30:16 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/62979">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto">I was experimenting with your regex a bit and I noticed that not only did it match the text inside the final &lt;tr&gt;&lt;/tr&gt; pair, but it also matched the &lt;/tr&gt; tag as well?</p>
</blockquote>
<p dir="auto">As I said it was from <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> solution for the first instance. Thus in his post:</p>
<p dir="auto">FIND = (?s)\A.<em>?&lt;tr&gt;\s</em>\K.<em>?(\s</em>&lt;/tr&gt;)<br />
REPLACE = new contents$1<br />
MODE = regular expression<br />
REPLACE ALL<br />
then I get</p>
<p dir="auto">So the replacement text would have been <code>new contents$1</code>, again same as the first instance solution. Sorry forgot to mention that.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62980</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62980</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Wed, 17 Feb 2021 02:30:16 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 02:26:18 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></p>
<p dir="auto">I was experimenting with your regex a bit and I noticed that not only did it match the text inside the final &lt;tr&gt;&lt;/tr&gt; pair, but it also matched the &lt;/tr&gt; tag as well?</p>
<p dir="auto">Peter’s and my regexes only matched what was inside; not sure if you were solving something Vasile wanted or not with that – not going back to read/revisit it! – but I took the liberty of tweaking yours a bit so it matches what ours does:</p>
<p dir="auto"><code>(?s)\A.*&lt;tr&gt;\K.+?(?=&lt;/tr&gt;)</code></p>
<p dir="auto">and that appears to be the shortest matching regex thus far.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62979</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62979</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 17 Feb 2021 02:26:18 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 01:10:22 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/62977">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto">…so much simpler…</p>
</blockquote>
<p dir="auto">Well, maybe.<br />
But nothing is going to beat your discussion of your thought process.<br />
An important factor in a good solution.</p>
<p dir="auto">I’ve always thought of the <code>((?!UNWANTED).)*</code> construct as somewhat “expensive”, but maybe that’s just because it “feels” complicated, but it would take a true regex genius like <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> to discuss that.</p>
<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></p>
<p dir="auto">Nice one as well!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62978</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62978</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 17 Feb 2021 01:10:22 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 01:00:55 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> and <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> ,</p>
<p dir="auto">Those are so much simpler than mine!  Congrats! 🎉👏👍</p>
<p dir="auto">Anyway, I am still glad I presented my solution, as it hopefully shows future readers a thought process that can arrive at a working regex, even if it’s not the simplest or most efficient.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62977</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62977</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Wed, 17 Feb 2021 01:00:55 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 00:34:25 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/62975">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto">BTW, I solved it on my own as well, for my own “pleasure”.</p>
</blockquote>
<p dir="auto">I also had, using <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> solution for the “first” instance, removing JUST 1 character. Maybe mine also has holes.<br />
<code>(?s)\A.*&lt;tr&gt;\s*\K.*?(\s*&lt;/tr&gt;)</code><br />
So turning a non-greedy regex into a greedy one. It firstly grabs everything, then backs up until the &lt;tr&gt;…&lt;/tr&gt; sequence is true. Even the <code>\A</code> sequence could be removed IF the cursor were in the first position of the open file.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62976</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62976</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Wed, 17 Feb 2021 00:34:25 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 00:27:30 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></p>
<p dir="auto">BTW, I solved it on my own as well, for my own “pleasure”.<br />
But I wasn’t going to post it, punishing, I guess, the 32.6K readers that were on the edge of their seat waiting for it – at the expense of those that I didn’t really want to have it.  But since you let the cat out of the bag, perhaps it is instructive to see a different approach:</p>
<p dir="auto"><code>(?s)&lt;tr&gt;.*&lt;/tr&gt;.*?&lt;tr&gt;\K.+?(?=&lt;/tr&gt;.*?\z)</code></p>
<p dir="auto">It seems to work; maybe there are holes.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62975</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62975</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 17 Feb 2021 00:27:30 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Wed, 17 Feb 2021 00:11:08 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></p>
<p dir="auto">Nice treatment, Peter.  +1 (or more).<br />
It’s an analysis by a thinking human being that has demonstrated he is capable of learning, adapting, and growing – wouldn’t it be great if everyone was like that?<br />
Thank you on behalf of 32.6K readers (if I may be so bold as to speak for them) for this and your other thoughtful and thorough contributions to the Notepad++ user Community, and of course the N++ user manual.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62974</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62974</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 17 Feb 2021 00:11:08 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Tue, 16 Feb 2021 22:59:58 GMT]]></title><description><![CDATA[<p dir="auto">Future readers,</p>
<p dir="auto">Since there historically have been a lot of reads on this discussion, it will be good to have the both the “first match” and “last match” versions of the <code>&lt;tr&gt;</code> question posed earlier, since it was brought up.  Also, I had forgotten that there was an advanced concept needed for the find-the-last that wasn’t needed in the regex for find-the-first, which even after reading the docs isn’t obvious.  So for teaching purposes, here is how I went about solving the problem.</p>
<p dir="auto">I started by saying, "I want from <code>&lt;tr&gt;</code> to <code>&lt;/tr&gt;</code>, then as few characters as possible between the <code>&lt;/tr&gt;</code> and the end.  I knew that wasn’t enough, but it was a starting point for my regex.  I translated that into <code>(?s)&lt;tr&gt;.*?&lt;/tr&gt;.*?\z</code> – which searches for literal text, then as few as possible of any character, then literal text, then as few as possible of any character followed by end of file.  That gets you close, but isn’t quite there, because “as few as possible” doesn’t guarantee that it won’t contain other <code>&lt;tr&gt;</code>, because of the way regex engines work.</p>
<p dir="auto">There’s a useful idiom of “multiple characters in a row, as long as they don’t contain some unwanted string”.  It actually took me a while to re-find this, because I lost my bookmark (and don’t use it often): <code>((?!UNWANTED).)*</code>, which can be described as: do a lookahead <code>(?!...)</code> where the text after here cannot match what’s inside here – in this case, the literal <code>UNWANTED</code>, but it could be an arbitrary regex itself – but that lookahead doesn’t consume any characters, so now we want to match exactly one character (I might call that, “capture one character if the UNWANTED lookahead isn’t found”); repeat this sequence 0 or more times.  For the purposes of the dummy example proposed by the previous questioner, <code>((?!&lt;tr&gt;).)*</code> says “find as many characters as you can, as long as they don’t contain <code>&lt;tr&gt;</code>”.</p>
<p dir="auto">We can use this in place of both of the <code>.*</code> in the regex above: <code>(?s)&lt;tr&gt;((?!&lt;tr&gt;).)*?&lt;/tr&gt;((?!&lt;tr&gt;).)*?\z</code>.  This gets us really close…</p>
<p dir="auto">But we’ve matched all of the text from the beginning of the final <code>&lt;tr&gt;</code> to the end of the document.  That will make it harder to replace just the contents of the <code>&lt;tr&gt;...&lt;/tr&gt;</code> pair.   It could be done with groups, but in this nested mass of parentheses, getting the right group number will be confusing. Instead, I will convert the first <code>&lt;tr&gt;</code> to a lookbehind <code>(?&lt;=&lt;tr&gt;)</code>, which says that <code>&lt;tr&gt;</code> must come immediately before our match, but not be in the match.  And I convert the ending <code>&lt;/tr&gt;</code> as well as everything that comes after that into a lookahead, so it’s not included in our match, either.  That brings our expression to <code>(?s)(?&lt;=&lt;tr&gt;)((?!&lt;/tr&gt;).)*?(?=&lt;/tr&gt;((?!&lt;tr&gt;).)+?\z)</code> – which, at first glance, looks really confusing; but when it was built up step-by-step, it’s not so bad.</p>
<p dir="auto">If I add in the <code>(?x)</code> (or, <code>(?sx)</code> to combine it), I can add some space and document it:</p>
<pre><code>(?xs)   (?&lt;=&lt;tr&gt;)      ((?!&lt;/tr&gt;).)*?      (?=&lt;/tr&gt;((?!&lt;tr&gt;).)+?\z)
        ^^^^^^^^^      ^^^^^^^^^^^^^^      ^^^^^^^^^^^^^^^^^^^^^^^^
        |              |                   `- this must come after our match
        |              `- this is what we really want to match; 
        |                 the &lt;tr&gt; is literal, the rest is syntax
        `- this must come before our match; the &lt;tr&gt; is literal, 
           the rest is syntax
</code></pre>
<p dir="auto">With this FIND expression, what you put in REPLACE just needs to be the literal new content for your final <code>&lt;tr&gt;</code> entry.</p>
<p dir="auto">so with the text</p>
<pre><code>&lt;other&gt;stuff&lt;/other&gt;
&lt;tr&gt;first&lt;/tr&gt;
&lt;tr&gt;second&lt;/tr&gt;
&lt;tr&gt;third&lt;/tr&gt;
&lt;tr&gt;4&lt;/tr&gt;
&lt;tr&gt;5&lt;/tr&gt;
&lt;tr&gt;6&lt;/tr&gt;
&lt;tr&gt;last&lt;/tr&gt;
&lt;other&gt;stuff&lt;/other&gt;
</code></pre>
<p dir="auto">FIND NEXT will just select <code>last</code>, and if you do a REPLACE (or REPLACE ALL), it will just replace that content.</p>
<p dir="auto">Personally, I would not expect a fresh newbie to regexes to get very far on this problem, but someone who has been using regular expressions for a few years, I would expect to get at least as far as <code>(?s)&lt;tr&gt;.*?&lt;/tr&gt;.*?\z</code>, so their question would be “I want to find the last <code>&lt;tr&gt;...&lt;/tr&gt;</code> pair in a document and replace its contents; I tried <code>(?s)&lt;tr&gt;.*?&lt;/tr&gt;.*?\z</code> because I thought that would find as little as possible between the open and close tag, and as little as possible between the close tag and the end of file, but it’s still grabbing all the <code>&lt;tr&gt;</code> pairs rather than just the last one; can you help me find what I’m doing wrong?”.</p>
<p dir="auto">It all looks difficult… but the more you use regular expressions, and the more you try to figure out new combinations of the pieces you know, the more you will understand how the bits and pieces work together.  Every time you come across a need for a new complicated search-and-replace expression, start with the pieces you know, look at the docs, try to add in the new bits you are trying to learn; ask <em>specific</em> questions (“here’s what I tried and why, but it didn’t work” or “here’s what I’ve tried so far, but I don’t know how to edit this regex so that XXXX” (where XXXX is specific) is infinitely better than “here’s my text; make a regex for me”), and every time you learn something new, put it in your mental bag of tricks.  (Sometimes, even for people like me who use regex a lot, the bag of tricks overflows, and you’ll have to re-figure out something you’ve done before; sometimes, the end result will look the same, but other times it will end up looking quite different… which is good, because you’ve learned a new way of doing it that you didn’t know before… and maybe this one will stick with you better than the one you forgot before.)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62973</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62973</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 16 Feb 2021 22:59:58 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Tue, 16 Feb 2021 21:36:49 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/62964">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto">&lt;tr&gt;</p>
</blockquote>
<p dir="auto">if someone knows how to select only the last instance of my request, please write the solution, not for me, but for those 36 k who have read this topic, and will read it from now on. Don’t do it for me, do it for other that search this solution.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62972</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62972</guid><dc:creator><![CDATA[Vasile Caraus]]></dc:creator><pubDate>Tue, 16 Feb 2021 21:36:49 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Tue, 16 Feb 2021 18:52:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: Vasile-Caraus">@<bdi>Vasile-Caraus</bdi></a> said in <a href="/post/62933">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto">your (?s)\b&lt;tr&gt;.+?&lt;/tr&gt;\b is not working :(</p>
</blockquote>
<p dir="auto">Now that I’m on a PC I can see the original post (recent) from you stating your regex was “finding both &lt;tr&gt;…&lt;/tr&gt;” was most likely <strong>false</strong>. Copying the examples I see that by using the <code>\b</code>, it actually prevented the regex from capturing the example text.</p>
<p dir="auto">So my first solution to add a <code>?</code> to your regex would have fixed the issue <code>IF</code> your regex was actually capturing too much. My later statement to remove the <code>\b</code> was in fact a correct move and would/should have resolved the issue. Except your statement about capturing the “first” only “&lt;tr&gt;…&lt;/tr&gt;” was a bit troubling as you didn’t have the <code>\A</code> anchor as <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> stated was needed. I assumed (wrongly possibly) that you possibly had a large html file in which you wanted to find (and replace?) the first “&lt;tr&gt;…&lt;/tr&gt;” and if using ONLY the “Find Next” or “Replace” button when cursor was at start of an open file would have found the first set.</p>
<p dir="auto">I guess I have learnt a valuable lesson, don’t provide answers when not able to independently confirm OP statements. I was on an android tablet late in the evening and about to go to bed when I saw this post and assumed it was an easy fix given the statement about it selecting too much text.</p>
<p dir="auto">Terry</p>
<p dir="auto">PS thanks Alan for coming to my rescue ;-))</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62971</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62971</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Tue, 16 Feb 2021 18:52:15 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Tue, 16 Feb 2021 16:33:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: Vasile-Caraus">@<bdi>Vasile-Caraus</bdi></a> said in <a href="/post/62965">Regex: Select only the first instance of search results / first match</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a>  it is easy for a developer to understand code, but is not easy for a painter to understand the code… :)</p>
</blockquote>
<p dir="auto">The whole point of CMS is to make it easier for painters to make a website, almost literally.  If you cannot learn the CMS, then you should probably hire an expert.</p>
<p dir="auto">Conversely, to your statement, if I go to my painter friend and ask him to paint me a picture to hang on the wall, he <em>might</em> do it for free one time, but after that, he’ll tell me “either learn to paint yourself, or pay me”.</p>
<p dir="auto">You know enough about regex to come up with some guesses, so you already know a lot of the code, and you have been pointed to the documentation to be able to learn more.  Try to piece together the bits you know in the right order for the problem you have.  I have given you hints as to the right order.  But at some point, you’re going to have to choose to learn, or find someone you can pay to do the development for your website.</p>
<p dir="auto">Because expecting us to be your personal free regular-expression writers for 4.5years, without ever contributing anything else to this forum, is …  Nevermind.</p>
<p dir="auto">Good luck.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62967</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62967</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 16 Feb 2021 16:33:16 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Select only the first instance of search results &#x2F; first match on Tue, 16 Feb 2021 16:27:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vasile-caraus" aria-label="Profile: Vasile-Caraus">@<bdi>Vasile-Caraus</bdi></a></p>
<blockquote>
<p dir="auto">it is easy for a developer to understand code, but is not easy for a painter to understand the code…</p>
</blockquote>
<p dir="auto">If you can’t stand the heat, get out of the kitchen.</p>
<hr />
<p dir="auto">Q: What’s the meaning of the phrase ‘If you can’t stand the heat, get out of the kitchen’?</p>
<p dir="auto">A: Don’t persist with a task if the pressure of it is too much for you. The implication being that, if you can’t cope, you should leave the work to someone who can.</p>
<hr />
<p dir="auto">It’s getting embarrassing for you.<br />
It’s almost an uncomfortable thing to witness.<br />
:-(</p>
]]></description><link>https://community.notepad-plus-plus.org/post/62966</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/62966</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 16 Feb 2021 16:27:06 GMT</pubDate></item></channel></rss>