<?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[[Notepad++] Remove everything except a specific string using regex]]></title><description><![CDATA[<p dir="auto">Hello everyone,<br />
I need support with regular expressions.</p>
<p dir="auto">I have a very large query from which I need to extract only the names of the tables used in the query. The tables have different names, but fortunately, they all follow the same pattern: database.schema.table_name.</p>
<p dir="auto">The table_name is always composed of 6 characters in total: 3 letters followed by 3 numbers. So the complete pattern is:</p>
<pre><code>database.schema.[A-Za-z]{3}\d{3}.
</code></pre>
<p dir="auto">I’d like to remove everything that is between one pattern and the next, replacing it with nothing, effectively removing joins and various conditions that are in between.</p>
<p dir="auto">For example, if I have:</p>
<pre><code>SELECT *
FROM database.schema.abc123
INNER JOIN database.schema.def456 on id1 = id2
LEFT JOIN database.schema.ghi789 on id2 = id3
WHERE 1=1
</code></pre>
<p dir="auto">I would like to have as a result:</p>
<pre><code>database.schema.abc123
database.schema.def456
database.schema.ghi789
</code></pre>
<p dir="auto">I made a first attempt using the regular expression:</p>
<pre><code>database.schema.[A-Za-z]{3}\d{3}(.|\s)*?database.schema.[A-Za-z]{3}\d{3}
</code></pre>
<p dir="auto">but it rightly includes in the selection also the string that I need to keep.</p>
<p dir="auto">How can I set the regular expression to select only what is between two strings (different from each other but with the same pattern) excluding the strings of interest from the selection?</p>
<p dir="auto">Thanks everyone for the support. 😊</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/26186/notepad-remove-everything-except-a-specific-string-using-regex</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 05:50:58 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/26186.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 10 Oct 2024 19:14:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Notepad++] Remove everything except a specific string using regex on Fri, 11 Oct 2024 06:03:45 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/francesco-vespari" aria-label="Profile: francesco-vespari">@<bdi>francesco-vespari</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/mkupper" aria-label="Profile: mkupper">@<bdi>mkupper</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">Here is my <strong>alternate</strong> solution :</p>
<p dir="auto">SEARCH <strong><code>(?-is)^(?:(?!data).)+\R|(?:(?!data).)+|(database\.schema\.[A-Za-z]{3}[0-9]{3})</code></strong></p>
<p dir="auto">REPLACE <strong><code>?1$1</code></strong></p>
<p dir="auto">This solution keeps any <strong>empty</strong> line which <strong>already</strong> exists in the <strong>present</strong> text :</p>
<p dir="auto">So, for example, given this <em>INPUT</em> text :</p>
<pre><code class="language-diff">SELECT *
FROM database.schema.ABC123
INNER JOIN database.schema.def456 on id1 = id2
LEFT JOIN database.schema.ghi789 on id2 = id3

WHERE 1=1

SELECT *
FROM database.schema.ABC123
INNER JOIN database.schema.def456 on id1 = id2
LEFT JOIN database.schema.ghi789 on id2 = id3
WHERE 1=1





SELECT *
FROM database.schema.ABC123
INNER JOIN database.schema.def456 on id1 = id2
LEFT JOIN database.schema.ghi789 on id2 = id3
WHERE 1=1
</code></pre>
<p dir="auto">we would get this <em>OUTPUT</em> one :</p>
<pre><code class="language-diff">database.schema.ABC123
database.schema.def456
database.schema.ghi789


database.schema.ABC123
database.schema.def456
database.schema.ghi789





database.schema.ABC123
database.schema.def456
database.schema.ghi789
</code></pre>
<hr />
<p dir="auto"><strong>Notes</strong> :</p>
<ul>
<li>
<p dir="auto">The <strong>first</strong> part of the search regex, after the <strong>modifiers</strong> <strong><code>(?-is)</code></strong>  is <strong><code>^(?:(?!data).)+\R</code></strong> which matches any <strong>complete</strong> line, with its <strong>line-break</strong>, <em>ONLY IF</em> it  does <em>NOT</em> contain the string <strong><code>data</code></strong>, with this <strong>case</strong>, at <strong>any</strong> position of <strong>current</strong> line</p>
</li>
<li>
<p dir="auto">The <strong>second</strong> alternative <strong><code>(?:(?!data).)+</code></strong> matches any part of line, which does <em>NOT</em> contain the string <strong><code>data</code></strong>, with this <strong>case</strong></p>
</li>
<li>
<p dir="auto">Until now, just note that <strong>no</strong> group has been defined because of the two <strong>non-capturing</strong> syntaxes <strong><code>(?:...</code></strong></p>
</li>
<li>
<p dir="auto">Finally, the <strong>third</strong> alternative is the string that we want to keep, i.e. the string <strong><code>(database\.schema\.[A-Za-z]{3}[0-9]{3})</code></strong>, with this <strong>exact</strong> case, which is stored as <strong>group <code>1</code></strong></p>
</li>
<li>
<p dir="auto">As we use a <strong>conditional</strong> replacement <strong><code>?1$1</code></strong>, which means : <em>ONLY</em> rewrite the <strong>group <code>1</code></strong> if this group is <strong>defined</strong>, we are just left with the <strong>expected</strong> text !</p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/97016</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/97016</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 11 Oct 2024 06:03:45 GMT</pubDate></item><item><title><![CDATA[Reply to [Notepad++] Remove everything except a specific string using regex on Thu, 10 Oct 2024 19:43:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/francesco-vespari" aria-label="Profile: Francesco-Vespari">@<bdi>Francesco-Vespari</bdi></a> said in <a href="/post/97013">[Notepad++] Remove everything except a specific string using regex</a>:</p>
<blockquote>
<p dir="auto">How can I set the regular expression to select only what is between two strings (different from each other but with the same pattern) excluding the strings of interest from the selection?</p>
</blockquote>
<p dir="auto">I’ll focus on the general idea, giving hints as to how to do it for your specific case.  But there are a plethora of ways.  For example, if in <code>A1BA2</code> you wanted to keep the “good” <code>A1</code> and <code>A2</code> and delete just the “bad” section <code>B</code>.  There would be two common ways I’d do such a task:</p>
<ul>
<li>you could capture the A1 and A2 into groups, and use those groups (rather than empty string) in the replacement: FIND = <code>(A1)B(A2)</code> , REPLACE = <code>$1$2</code></li>
<li>you could use a combination of <code>\K</code> <a href="https://npp-user-manual.org/docs/searching/#control-flow" rel="nofollow ugc">control flow escape</a> (which says the stuff to the left of this symbol must match, but I don’t want it replaced by the replacement) and a <a href="https://npp-user-manual.org/docs/searching/#assertions" rel="nofollow ugc">lookahead assertion</a>: FIND = <code>A1\KB(?=A2)</code> , REPLACE = empty</li>
</ul>
<p dir="auto">(<code>A1</code>, <code>B</code>, and <code>A2</code> are meant to be placeholders, which would need to be real regex</p>
<p dir="auto">For your specific case, the <code>B</code> would be your <code>(.|\s)*?</code> and the <code>A1</code> and <code>A2</code> would be the surrounding stuff.</p>
<p dir="auto">A few other specific notes on your attempted regex:</p>
<blockquote>
<p dir="auto"><code>database.schema.[A-Za-z]{3}\d{3}(.|\s)*?database.schema.[A-Za-z]{3}\d{3}</code></p>
</blockquote>
<ul>
<li>the <code>.</code> between the tokens will match anything, not just a literal dot character.  so <code>database-schema-</code> would also match; use <code>\.</code> to match literal dot.</li>
<li><code>(.|\s)*?</code> could be written as just <code>.*?</code> – <code>.</code> already matches a normal space.  If you want that to also match across lines, you can use the <code>. matches newline</code> checkmarked, or prefix your whole regex with <code>(?s)</code></li>
<li>since the <code>A1</code> and <code>A2</code> portions of your regex are the same regex code, you can make use of <a href="https://npp-user-manual.org/docs/searching/#control-flow" rel="nofollow ugc">numbered recursion</a> – so my example regex could be <code>(A1)\KB(?=(?1))</code> which puts the regex <code>A1</code> into group#1, then the <code>(?1)</code> inside the lookahead says “use the same regex as for group#1, but use it here”</li>
</ul>
]]></description><link>https://community.notepad-plus-plus.org/post/97015</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/97015</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Thu, 10 Oct 2024 19:43:56 GMT</pubDate></item><item><title><![CDATA[Reply to [Notepad++] Remove everything except a specific string using regex on Thu, 10 Oct 2024 19:28:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/francesco-vespari" aria-label="Profile: Francesco-Vespari">@<bdi>Francesco-Vespari</bdi></a> I would do it as two separate search/replace passes.</p>
<p dir="auto">The first removes all lines except those that start with <code>FROM</code>.<br />
<strong>Search: <code>(?-i)^((?!FROM ).*\R)+</code><br />
Replace: <code>(blank or nothing)</code></strong></p>
<p dir="auto">With that I’m doing <code>(?!FROM ).*\R</code> which removes one line at a time but wrapped that in an extra <code>()+</code> as Notepad++ tends to take its time doing a delete regardless of how large it is.  The <code>()+</code> deletes multiple consecutive lines that don’t start with <code>FROM</code></p>
<p dir="auto">Once you have that then your original regular expression should work well as there is no extraneous noise in the file.</p>
<p dir="auto">It can be done in one search/replace but it’s much harder to understand and maintain than the two-pass method that first removes the noise.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/97014</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/97014</guid><dc:creator><![CDATA[mkupper]]></dc:creator><pubDate>Thu, 10 Oct 2024 19:28:42 GMT</pubDate></item></channel></rss>