<?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: How to find a duplicate tag on consecutive lines?]]></title><description><![CDATA[<p dir="auto">hi, in the code below, you will see that two lines are repeated one after the other, which is a problem:</p>
<pre><code>  &lt;/url&gt;
 &lt;/url&gt;
</code></pre>
<p dir="auto">The code:</p>
<pre><code>&lt;url&gt;
  &lt;loc&gt;https://my-website.com/en/example-love.html&lt;/loc&gt;
  &lt;lastmod&gt;2018-11-30T17:19:37+00:00&lt;/lastmod&gt;
 &lt;changefreq&gt;weekly&lt;/changefreq&gt;
  &lt;priority&gt;0.6400&lt;/priority&gt;
&lt;/url&gt;
  &lt;/url&gt;
  &lt;url&gt;
  &lt;loc&gt;https://my-website.com/en/my-cat-is-here.html&lt;/loc&gt;
  &lt;lastmod&gt;2018-11-30T17:19:37+00:00&lt;/lastmod&gt;
 &lt;changefreq&gt;weekly&lt;/changefreq&gt;
  &lt;priority&gt;0.6400&lt;/priority&gt;
&lt;/url&gt;
</code></pre>
<p dir="auto">I want to find out the <code>rss.xml</code> file that contains a duplicate <code>&lt;/url&gt;</code> on consecutive lines.</p>
<p dir="auto">I don’t know why my regex is not working:</p>
<p dir="auto">FIND: <code>(?-i:&lt;/url&gt;|(?!\A)\G)(?s-i:(?!&lt;url&gt;).)*?\K(?-i:(?!&lt;/url&gt;))</code><br />
Replace by: <code>(Leave empty)</code></p>
]]></description><link>https://community.notepad-plus-plus.org/topic/21388/regex-how-to-find-a-duplicate-tag-on-consecutive-lines</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 23:53:29 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/21388.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 Jun 2021 11:58:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Mon, 28 Jun 2021 15:28:48 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> great answer, thanks !</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67485</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67485</guid><dc:creator><![CDATA[Robin Cruise]]></dc:creator><pubDate>Mon, 28 Jun 2021 15:28:48 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Tue, 29 Jun 2021 02:29:23 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/robin-cruise" aria-label="Profile: Robin-Cruise">@<bdi>Robin-Cruise</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">Ah, <strong>interesting</strong> point !</p>
<p dir="auto">Let’s work against this <strong>simple</strong> line, pasted in a <strong>new</strong> tab :</p>
<pre><code>----abc12345abc----def67890def----abc12345def----def67890abc----
</code></pre>
<ul>
<li>
<p dir="auto">The regex <strong><code>(?-i)(abc)\d+\1|(def)\d+\2</code></strong> <strong>correctly</strong> matches the strings <strong>abc12345abc</strong> and <strong>def67890def</strong> :</p>
<ul>
<li>
<p dir="auto">The <strong>group <code>1</code></strong> is the string <strong><code>abc</code></strong>, <strong>re</strong>-used in the search regex as the <strong>back-reference</strong> <strong><code>\1</code></strong>, in its <strong>first</strong> alternative <strong><code>(abc)\d+\1</code></strong></p>
</li>
<li>
<p dir="auto">The <strong>group <code>2</code></strong> is the string <strong><code>def</code></strong>, <strong>re</strong>-used in the search regex as the <strong>back-reference</strong> <strong><code>\2</code></strong>, in its <strong>second</strong> alternative <strong><code>(def)\d+\2</code></strong></p>
</li>
<li>
<p dir="auto">Each <strong>back-reference</strong> to a group, <strong><code>\1</code></strong> and <strong><code>\2</code></strong>, is totally <strong>defined</strong> when using each <strong>alternative</strong></p>
</li>
</ul>
</li>
<li>
<p dir="auto">The <strong>almost</strong> identical regex <strong><code>(?-i)(abc)\d+\1|(def)\d+\1</code></strong> just matches the string <strong>abc12345abc</strong> ! Why ?</p>
<ul>
<li>
<p dir="auto">The <strong>group <code>1</code></strong> is the string <strong><code>abc</code></strong>, <strong>re</strong>-used in the search regex as the <strong>back-reference</strong> <strong><code>\1</code></strong>, in its <strong>first</strong> alternative <strong><code>(abc)\d+\1</code></strong></p>
</li>
<li>
<p dir="auto">The <strong>group <code>2</code></strong> is the string <strong><code>def</code></strong>, <strong>not</strong> re-used in this search regex</p>
</li>
<li>
<p dir="auto">When trying the <strong>second</strong> alternative <strong><code>(def)\d+\1</code></strong>, the regex engine does <strong>not</strong> know the <strong>back-reference</strong> <strong><code>\1</code></strong> which refers to <strong>group <code>1</code></strong> ( the string <strong><code>abc</code></strong> ), defined in the <strong>first</strong> alternative. Thus, this <strong>second</strong> alternative will <strong>never</strong> match !</p>
</li>
</ul>
</li>
<li>
<p dir="auto">However, note that :</p>
<ul>
<li>
<p dir="auto">The regex <strong><code>(?i)(def)\d+\1</code></strong>, <strong>only</strong>, would normally match the string <strong>def67890def</strong></p>
</li>
<li>
<p dir="auto">The regex <strong><code>(?i)(def)\d+\2</code></strong>, <strong>only</strong>, outputs the message <strong><code>Invalid regular expression</code></strong>, as <strong>no</strong> group <strong><code>2</code></strong> is <strong>defined</strong> in this regex</p>
</li>
</ul>
</li>
<li>
<p dir="auto">Now, let’s use the <strong><code>\K</code></strong> syntax in the regex <strong><code>(?-i)(abc)\d+\1|(def)\d+\2.+\Kabc\d+\2</code></strong></p>
<ul>
<li>
<p dir="auto">The <strong>group <code>1</code></strong> is the string <strong><code>abc</code></strong>, <strong>re</strong>-used in the search regex as the <strong>back-reference</strong> <strong><code>\1</code></strong>, in its <strong>first</strong> alternative <strong><code>(abc)\d+\1</code></strong></p>
</li>
<li>
<p dir="auto">The <strong>group <code>2</code></strong> is the string <strong><code>def</code></strong>, <strong>re</strong>-used in the search regex as the <strong>back-reference</strong> <strong><code>\2</code></strong>, in its <strong>second</strong> alternative <strong><code>(def)\d+\2.+\Kabc\d+\2</code></strong></p>
</li>
<li>
<p dir="auto">The <strong>first</strong> alternative, <strong><code>(abc)\d+\1</code></strong>, as above, matches the string <strong>abc12345abc</strong></p>
</li>
<li>
<p dir="auto">The <strong>second</strong> alternative, <strong><code>(def)\d+\2.+\Kabc\d+\2</code></strong> first matches the <strong>def67890def</strong> string and defines the <strong>group <code>2</code></strong> ( <strong><code>def</code></strong> )  then, further on, after <strong>reset</strong> of search by the <strong><code>\K</code></strong> feature, matches the string <strong>abc12345def</strong> because the groups defined, <strong>before</strong> <strong><code>\K</code></strong>, are still defined <strong>after</strong> <strong><code>\K</code></strong> !</p>
</li>
</ul>
</li>
<li>
<p dir="auto">Finally, let’s consider the regex <strong><code>(?-i)(abc)\d+\1|(def)\d+\2|abc\d+\2|def\d+\1</code></strong>, composed of <strong><code>4</code></strong> <strong>alternatives</strong></p>
<ul>
<li>
<p dir="auto">Only the <strong>first two</strong> alternatives match, successively, the <strong>two</strong> strings <strong>abc12345abc</strong> and <strong>def67890def</strong></p>
</li>
<li>
<p dir="auto">In the <strong>last two</strong> alternatives, the <strong>back-references</strong> <strong><code>\1</code></strong> and <strong><code>\2</code></strong> refer to a <strong>not defined</strong> group, when executing the <strong>third</strong> / <strong>fourth</strong> alternative !</p>
</li>
</ul>
</li>
<li>
<p dir="auto">Now, if, instead of the <strong>back-references</strong> <strong><code>\1</code></strong> and <strong><code>\2</code></strong>, we use the <strong>subroutines calls</strong> <strong><code>(?1)</code></strong> and <strong><code>(?2)</code></strong>, which represent the <strong>exact</strong> regexes in groups <strong><code>1</code></strong> and <strong><code>2</code></strong> ( so the strings <strong><code>abc</code></strong> and <strong><code>def</code></strong> ), we get the regex <strong><code>(?-i)(abc)\d+\1|(def)\d+\2|abc\d+(?2)|def\d+(?1)</code></strong> which do match the <strong>four</strong> strings : <strong>abc12345abc</strong>, <strong>def67890def</strong>, <strong>abc12345def</strong> and <strong>def67890abc</strong></p>
<ul>
<li>As you can see, <strong>subroutine calls</strong>, <strong><code>(?#)</code></strong> are <strong>defined</strong> by the regex engine <strong>before</strong> executing any <strong>alternative</strong> and, thus, are defined in <strong>any</strong> part of the overall regex, <strong>where</strong> they occur. You may <strong>even</strong> create a regex containing a <strong>subroutine call</strong> to a group, which comes <strong>next</strong> ! For instance, the regex <strong><code>(?1):(?1):(\d\d)</code></strong> would match the string <strong>03:05:45</strong> or <strong>11:52:17</strong> And just compare with the regex <strong><code>\1:\1:(\d\d)</code></strong> which is totally <strong>invalid</strong> !</li>
</ul>
</li>
</ul>
<hr />
<p dir="auto">Back to <strong>your</strong> questions, <a class="plugin-mentions-user plugin-mentions-a" href="/user/robin-cruise" aria-label="Profile: robin-cruise">@<bdi>robin-cruise</bdi></a>, you said :</p>
<blockquote>
<p dir="auto">But if I make a regex search, something like (&lt;loc&gt;).*\1 it doesn’t work, seems that notepad++ cannot find the text.</p>
</blockquote>
<p dir="auto">Sorry, but it <strong>does</strong> work !</p>
<p dir="auto">Try the regex <strong><code>(?s)(&lt;loc&gt;).*\1</code></strong> against this text :</p>
<pre><code class="language-diff">&lt;loc&gt;
bla
bla
blah
&lt;loc&gt;
</code></pre>
<p dir="auto">As about the <strong><code>(&lt;loc&gt;).*|\1\R</code></strong> regex, obviously, <strong>only</strong> the <strong>first</strong> alternative can match. Indeed, the <strong>second</strong> alternative <strong><code>\1\R</code></strong> contains a <strong>back-reference</strong> <strong><code>\1</code></strong> to <strong>group <code>1</code></strong>, which is <strong>not</strong> defined in <em>THIS</em> alternative !</p>
<p dir="auto">Finally, your <strong><code>(&lt;loc&gt;).*|\1\R</code></strong> regex is simply <strong>equivalent</strong> to one the <strong>two</strong> forms :</p>
<ul>
<li>
<p dir="auto"><strong><code>(?s)(&lt;loc&gt;).*</code></strong></p>
</li>
<li>
<p dir="auto"><strong><code>(?-s)(&lt;loc&gt;).*</code></strong></p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67471</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67471</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 29 Jun 2021 02:29:23 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Sun, 27 Jun 2021 21:02:28 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/67395">Regex: How to find a duplicate tag on consecutive lines?</a>:</p>
<blockquote>
<p dir="auto">(?-i)(&lt;/url&gt;)\s+\K\1\R</p>
</blockquote>
<p dir="auto">thanks. But, can you tell me, when can I use that <code>\1</code> in the FIND option? Because I usually use it at the replace section, not at the FIND.</p>
<p dir="auto">ok, I know that <code>\1</code> refers to the first bracket <code>(&lt;/url&gt;)</code>. But if I make a regex search, something like <code>(&lt;loc&gt;).*\1</code> it doesn’t work, seems that notepad++ cannot find the text.</p>
<p dir="auto">I try, also, <code>(&lt;loc&gt;).*|\1\R</code></p>
<p dir="auto">so, when exactly can I use <code>\1</code> or <code>\2</code> in the FIND option?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67448</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67448</guid><dc:creator><![CDATA[Robin Cruise]]></dc:creator><pubDate>Sun, 27 Jun 2021 21:02:28 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Fri, 25 Jun 2021 18:20:02 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/robin-cruise" aria-label="Profile: robin-cruise">@<bdi>robin-cruise</bdi></a>,</p>
<p dir="auto">Yes, I made <strong>two</strong> little <strong>mistakes</strong> but you could have found the problem by <strong>yourself</strong> !</p>
<ul>
<li>
<p dir="auto">First I forgot the <strong><code>(?-i)</code></strong> syntax, at the <strong>very beginning</strong> of the regex to <strong>ensures</strong> that all the process will be <strong>case</strong> sensitive !</p>
</li>
<li>
<p dir="auto">Secondly, my <strong>previous</strong> regex <strong><code>(&lt;/url&gt;)\s+\K\1</code></strong> first searches for the <strong><code>&lt;url&gt;</code></strong> string, according to the <strong><code>match case</code></strong> option, then some <strong>regex space</strong> class characters ( so any <strong>non-null</strong> range of <strong>space</strong>, <strong>tab</strong>, and/or <strong>End of Line</strong> chars ), then the <strong><code>&lt;/url&gt;</code></strong> string , according to the <strong><code>match case</code></strong> option, too. But I forgot to search for the <strong>line-break</strong> chars after <strong><code>&lt;/url&gt;</code></strong>, which can be achieved with the <strong><code>\R</code></strong> syntax</p>
</li>
</ul>
<p dir="auto">Thus, the <strong>exact</strong> regex to use is <strong><code>(?-i)(&lt;/url&gt;)\s+\K\1\R</code></strong> and, of course, click on the <strong><code>Replace All</code></strong> button, <strong>only</strong> ( <strong>not</strong> on the <strong>Replace</strong> one, due to the presence of the <strong><code>\K</code></strong> construction )</p>
<p dir="auto">Cheers,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67395</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67395</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 25 Jun 2021 18:20:02 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Fri, 25 Jun 2021 13:49:37 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/67363">Regex: How to find a duplicate tag on consecutive lines?</a>:</p>
<blockquote>
<p dir="auto">(&lt;/url&gt;)\s+\K\1</p>
</blockquote>
<p dir="auto">hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> If I make the search and replace, I will have an empty line between tags. Such as:</p>
<pre><code>&lt;/url&gt;
  
  &lt;url&gt;
</code></pre>
<p dir="auto">How can I modify those 2 regex you made, as not to have an empty line after replacement?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67371</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67371</guid><dc:creator><![CDATA[Robin Cruise]]></dc:creator><pubDate>Fri, 25 Jun 2021 13:49:37 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Fri, 25 Jun 2021 13:22:48 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> and <strong>All</strong>,</p>
<p dir="auto"><strong>Alan</strong>, I’m not <strong>fooled</strong> and I know for a fact that some people are <strong>just</strong> showing us a <strong>regular</strong> expression, loosely <strong>deduced</strong> from a previous regex, <strong>already</strong> posted on the forum, serving as an <strong>alibi</strong> for a <strong>supposed</strong> search !</p>
<p dir="auto">But, well: I had the opportunity, at the <strong>same</strong> time, in a <strong>short</strong> reply, to <strong>correct</strong> the OP’s <strong>version</strong> and to indicate the solution to the <strong>Robin</strong>’s question, <strong>correctly</strong> expressed !</p>
<p dir="auto">BR</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67367</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67367</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 25 Jun 2021 13:22:48 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Fri, 25 Jun 2021 12:52:21 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/67363">Regex: How to find a duplicate tag on consecutive lines?</a>:</p>
<blockquote>
<p dir="auto">Now, why do you bother to use this complicated regex syntax ?</p>
</blockquote>
<p dir="auto">I suspect the reason for that is the OP just pasted some regex because our policy to get (repeat) help here is to show-us-what-you-tried-first.</p>
<p dir="auto">And currently, OP is our champion of write-my-regex-for-me.</p>
<p dir="auto">So it has been paste-anything-because-that-ticks-the-box-and-now-I-will-get-the-help-I-need.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67364</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67364</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Fri, 25 Jun 2021 12:52:21 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: How to find a duplicate tag on consecutive lines? on Fri, 25 Jun 2021 12:48:14 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/robin-cruise" aria-label="Profile: robin-cruise">@<bdi>robin-cruise</bdi></a>,</p>
<p dir="auto">The <strong>correct</strong> regex is :</p>
<p dir="auto"><strong><code>(?-i:&lt;/url&gt;|(?!\A)\G)(?s-i:(?!&lt;url&gt;).)*?\K(?-i:&lt;/url&gt;)</code></strong></p>
<p dir="auto">Indeed, as <strong>your</strong> search regex <strong>ends</strong> with <strong><code>\K(?-i:(?!&lt;/url&gt;))</code></strong>, this means that you’re looking for an <strong>empty</strong> string, <strong>not</strong> followed by <strong><code>&lt;/url&gt;</code></strong> with this <strong>exact</strong> case. <strong>Not</strong> what you were expected too !</p>
<hr />
<p dir="auto">Now, <strong>why</strong> do you bother to use this <strong>complicated</strong> regex syntax ?</p>
<p dir="auto">Simply use this <strong>shorter</strong> regex S/R :</p>
<p dir="auto">SEARCH <strong><code>(&lt;/url&gt;)\s+\K\1</code></strong></p>
<p dir="auto">REPLACE <strong><code>Leave EMPTY</code></strong></p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/67363</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/67363</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 25 Jun 2021 12:48:14 GMT</pubDate></item></channel></rss>