<?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: Insert a new line in files, only if that line not exist in files]]></title><description><![CDATA[<p dir="auto">In almost all my hml files, I have this 2 lines:</p>
<p dir="auto"><code>&lt;meta property="og:title" content="My Title"/&gt;</code><br />
<code>&lt;meta property="og:description" content="My description of article"/&gt;</code></p>
<p dir="auto">But I made a search with regex, to find all files that does not contain the second line:</p>
<p dir="auto">SEARCH: <code>(?s)\A(?!.*(meta property="og:description).*$)</code></p>
<p dir="auto">So, seems I have some files that are missing the second line. I don’t want to make a simple replacement, because I can get 2 lines idendical.</p>
<p dir="auto">So, I need to find only those files that contain only the first line, and add to all this the second line</p>
<p dir="auto">So, I try this solution, but does not working:</p>
<p dir="auto">SEARCH: <code>(?s)\A(?!.*(meta property="og:description).*$)</code></p>
<p dir="auto">REPLACE BY: <code>\1</code></p>
]]></description><link>https://community.notepad-plus-plus.org/topic/22007/regex-insert-a-new-line-in-files-only-if-that-line-not-exist-in-files</link><generator>RSS for Node</generator><lastBuildDate>Sun, 17 May 2026 02:12:36 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/22007.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 18 Oct 2021 20:00:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Tue, 19 Oct 2021 10:26:35 GMT]]></title><description><![CDATA[<p dir="auto">thanks <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> and <a class="plugin-mentions-user plugin-mentions-a" href="/user/robin-cruise" aria-label="Profile: Robin-Cruise">@<bdi>Robin-Cruise</bdi></a></p>
]]></description><link>https://community.notepad-plus-plus.org/post/70650</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70650</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Tue, 19 Oct 2021 10:26:35 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Tue, 19 Oct 2021 09:56:50 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: hellena-crainicu">@<bdi>hellena-crainicu</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">Ah…OK. But, as your <strong>first</strong> regex attempt contains the <strong><code>\A</code></strong> assertion, relative to the <strong>very beginning</strong> of file, I <strong>wrongly</strong> supposed that the line <strong><code>&lt;meta property="og:title" content="My Title"/&gt;</code></strong> was always the <strong>first</strong> line of the file :-(</p>
<p dir="auto">And, from your screen-shot, the line <strong><code>&lt;meta property="og:title" content="My Title"/&gt;</code></strong> may also be the <strong>last</strong> line of current file, even <strong>without</strong> any <strong>line-ending</strong> char !!</p>
<hr />
<p dir="auto">So here is a <strong>new</strong> version which covers <strong>all</strong> cases possible :</p>
<p dir="auto">SEARCH <strong><code>(?-is)^&lt;meta property="og:title".+(?:\K(\z)|\R\K(?!&lt;meta property="og:description"))</code></strong></p>
<p dir="auto">REPLACE <strong><code>(?1\r\n)&lt;meta property="og:description" content="My description of article"/&gt;\r\n</code></strong></p>
<p dir="auto">So, <strong>each</strong> time that the regex engine meets the string <strong><code>&lt;meta property="og:title"</code></strong>, with this <strong>exact</strong> case, <strong>beginning</strong> a line, it inserts, <strong>right after</strong>, the new line <strong><code>&lt;meta property="og:description" content="My description of article"/&gt;</code></strong>, with this <strong>exact</strong> case + its <strong>line-break</strong> chars, but <em>ONLY IF</em> not <strong>already</strong> followed with a line beginning with <strong><code>&lt;meta property="og:description"</code></strong> OR IF it is located at the <strong>very end</strong> of file</p>
<p dir="auto">BR</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70649</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70649</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 19 Oct 2021 09:56:50 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Tue, 19 Oct 2021 05:45:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a><br />
Your screen print shows the Find Next button as being active, that is why I asked.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70641</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70641</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Tue, 19 Oct 2021 05:45:47 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Tue, 19 Oct 2021 05:45:40 GMT]]></title><description><![CDATA[<p dir="auto">Another simple solution, by two steps:</p>
<p dir="auto"><strong>1. Add the line you want after the one with <code>og:title</code> in all your files:</strong></p>
<p dir="auto">SEARCH: <code>(&lt;meta property="og:title" content=".*"/&gt;)</code></p>
<p dir="auto">REPLACE BY:  <code>\1\r&lt;meta property="og:description" content="My description of article"/&gt;</code></p>
<p dir="auto"><strong>2. Delete all duplicate lines</strong></p>
<p dir="auto">SEARCH: <code>(?-s)(^.*\R)\1+</code></p>
<p dir="auto">REPLACE BY: <code>\1</code></p>
]]></description><link>https://community.notepad-plus-plus.org/post/70640</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70640</guid><dc:creator><![CDATA[Robin Cruise]]></dc:creator><pubDate>Tue, 19 Oct 2021 05:45:40 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Tue, 19 Oct 2021 05:42:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> of course I press <code>Replace All</code> button</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70638</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70638</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Tue, 19 Oct 2021 05:42:01 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Tue, 19 Oct 2021 05:36:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a> said in <a href="/post/70636">Regex: Insert a new line in files, only if that line not exist in files</a>:</p>
<blockquote>
<p dir="auto">something suspicios, <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> See this print screen please. I try your both regex formulas.</p>
</blockquote>
<p dir="auto">Did you press the <code>Replace All</code> button as requested. As the regex uses the <code>\K</code> function clicking on any other option may cause the issue you see.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70637</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70637</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Tue, 19 Oct 2021 05:36:32 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Tue, 19 Oct 2021 05:28:23 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/70626">Regex: Insert a new line in files, only if that line not exist in files</a>:</p>
<blockquote>
<p dir="auto">(?-is)\A&lt;meta property=“og:title”.+\R\K(?!&lt;meta property=“og:description”)</p>
</blockquote>
<p dir="auto">something suspicios, <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> See this print screen please. I try your both regex formulas.</p>
<p dir="auto"><img src="https://camo.nodebb.org/2dc2b49068ea4b2ef83b0536c2ebb27d1a9e5de2?url=https%3A%2F%2Fsnipboard.io%2FHihQOc.jpg" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://community.notepad-plus-plus.org/post/70636</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70636</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Tue, 19 Oct 2021 05:28:23 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 22:33:58 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: hellena-crainicu">@<bdi>hellena-crainicu</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: terry-r">@<bdi>terry-r</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">If your line <strong><code>&lt;meta property="og:description"••••••••••</code></strong> follows <strong>immediately</strong> the line <strong><code>&lt;meta property="og:title"••••••••••/&gt;</code></strong>, a possible solution<br />
would be :</p>
<p dir="auto">SEARCH <strong><code>(?-is)\A&lt;meta property="og:title".+\R\K(?!&lt;meta property="og:description")</code></strong></p>
<p dir="auto">REPLACE <strong><code>&lt;meta property="og:description" content="My description of article"/&gt;\r\n</code></strong></p>
<p dir="auto">The <em>SEARCH</em> regex will match a <strong>zero length</strong> string, right after the <strong>first</strong> line <strong><code>&lt;meta property="og:title" content="My Title"/&gt;</code></strong>, <strong>whatever</strong> its title, <em>ONLY IF</em> it is <strong>not</strong> followed with a <strong>second</strong> line <strong><code>&lt;meta property="og:description" content="My description of article"/&gt;</code></strong>, <strong>whatever</strong> its description</p>
<p dir="auto">Then, the <em>REPLACE</em> regex simply inserts, at this <strong>empty</strong> location, a <strong>complete</strong> line <strong><code>&lt;meta property="og:description" content="My description of article"/&gt;</code></strong> followed with the usual <strong>Windows</strong> line-break chars <strong><code>\r\n</code></strong> ( or use <strong><code>\n</code></strong> only, if you deal with <strong>UNIX</strong> files )</p>
<hr />
<p dir="auto">So, the road map is :</p>
<ul>
<li>
<p dir="auto">Open the <strong>Replace</strong> dialog ( <strong><code>Ctrl + H</code></strong> )</p>
</li>
<li>
<p dir="auto">SEARCH <strong><code>(?-is)\A&lt;meta property="og:title".+\R\K(?!&lt;meta property="og:description")</code></strong></p>
</li>
<li>
<p dir="auto">REPLACE <strong><code>&lt;meta property="og:description" content="My description of article"/&gt;\r\n</code></strong>     Replace the part <strong><code>My description of article</code></strong> by the <strong>real</strong> text, of course !</p>
</li>
<li>
<p dir="auto">Tick the <strong><code>Wrap around</code></strong> option, only ( <em>IMPORTANT</em> )</p>
</li>
<li>
<p dir="auto">Select the <strong><code>Regular expression</code></strong> search mode</p>
</li>
<li>
<p dir="auto">Click on the <strong><code>Replace All</code></strong> button ( Do <strong>not</strong> use the <strong><code>Replace</code></strong> button ! )</p>
</li>
</ul>
<hr />
<p dir="auto">Of course, <strong>test</strong> the <em>matches</em> and the <em>replacements</em>, produced by this regex S/R, on <strong>one</strong> or <strong>few</strong> files, before applying it on a <strong>large</strong> bunch of files ;-))</p>
<p dir="auto">Best Regards</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70626</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70626</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Mon, 18 Oct 2021 22:33:58 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 20:53:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a> said in <a href="/post/70621">Regex: Insert a new line in files, only if that line not exist in files</a>:</p>
<blockquote>
<p dir="auto">I don’t understand…</p>
</blockquote>
<p dir="auto">Did you see the top post in the “Help Wanted” section (which is where your post is)? Its’ titled Please Read Before Posting and it’s <a href="https://community.notepad-plus-plus.org/topic/21965/please-read-before-posting">here</a>. It also references another post on how to format, which is what you need to read next.</p>
<p dir="auto">I strongly suggest you read that as it explains very well how examples are meant to be inserted into posts. Without it we (those who want to help) find it very difficult to get the solution without a lot of back and forth with the original poster, that’s you.</p>
<p dir="auto">In your case we’d need a few lines showing what the file (which is missing the text) looks like before the insert, and another example showing what it looks like afterwards.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70624</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70624</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 18 Oct 2021 20:53:05 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 20:22:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> said in <a href="/post/70620">Regex: Insert a new line in files, only if that line not exist in files</a>:</p>
<blockquote>
<p dir="auto">I’ll repeat my request. To insert examples, copy them into the posting area, select and then hit the &lt;/&gt; button above. This encapsulates the examples inside a black box, which helps to prevent the posting engine from altering some of the characters, most notably the (single/double)quote.<br />
Without examples it will be difficult for anyone to help you.</p>
</blockquote>
<p dir="auto">I don’t understand…</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70621</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70621</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Mon, 18 Oct 2021 20:22:54 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 20:18:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/terry-r" aria-label="Profile: Terry-R">@<bdi>Terry-R</bdi></a> said in <a href="/post/70617">Regex: Insert a new line in files, only if that line not exist in files</a>:</p>
<blockquote>
<p dir="auto">I think you need to supply an example of what this area looks like without the text you are looking for, specifically does it already have the " and/or the space before content. Without that it’s difficult to establish exactly what the regex needs to look like.</p>
</blockquote>
<p dir="auto">I’ll repeat my request. To insert examples, copy them into the posting area, select and then hit the <code>&lt;/&gt;</code> button above. This encapsulates the examples inside a black box, which helps to prevent the posting engine from altering some of the characters, most notably the (single/double)quote.</p>
<p dir="auto">Without examples it will be difficult for anyone to help you.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70620</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70620</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 18 Oct 2021 20:18:32 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 20:14:43 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> very close answer. So, I try to pur an <code>\r\n</code></p>
<p dir="auto">SEARCH: <code>(?s)\A(?!.(meta property="og:description).$)</code><br />
REPLACE BY: <code>\1\r\1(&lt;meta property="og:description" content="My description of article"/&gt;)</code></p>
<p dir="auto">but this will add the line before, not after the first line.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70619</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70619</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Mon, 18 Oct 2021 20:14:43 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 20:11:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a> said in <a href="/post/70616">Regex: Insert a new line in files, only if that line not exist in files</a>:</p>
<blockquote>
<p dir="auto">I also try this solution of yours. Doesn’t work… :(</p>
</blockquote>
<p dir="auto">I think you need to supply an example of what this area looks like without the text you are looking for, specifically does it already have the <code>"</code> and/or the <code>space</code> before <code>content</code>. Without that it’s difficult to establish exactly what the regex needs to look like.</p>
<p dir="auto">But on first look it needs position the cursor at the point where the insert will take place. So either capture the first portion of the file before the insert position as group 1 (<code>\1</code>) then in the replacement field have <code>\1(text here to be inserted)</code> or something similar such as using <code>\K</code> which is an advanced regex function.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70617</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70617</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 18 Oct 2021 20:11:36 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 20:06:02 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> I also try this solution of yours. Doesn’t work… :(</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70616</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70616</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Mon, 18 Oct 2021 20:06:02 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Insert a new line in files, only if that line not exist in files on Mon, 18 Oct 2021 20:04:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a> said in <a href="/post/70614">Regex: Insert a new line in files, only if that line not exist in files</a>:</p>
<blockquote>
<p dir="auto">So, I need to find only those files that contain only the first line, and add to all this the second line<br />
So, I try this solution, but does not working:<br />
SEARCH: (?s)\A(?!.<em>(meta property="og:description).</em>$)<br />
REPLACE BY: \1</p>
</blockquote>
<p dir="auto">When you have text within the <code>()</code> it allows you to capture it <code>IF</code> the text exists in the search area. Since you say the text is missing from this file, then it doesn’t capture anything, hence cannot return that text. I’d say replace your <code>\1</code> with the actual text to insert.</p>
<p dir="auto">Terry</p>
]]></description><link>https://community.notepad-plus-plus.org/post/70615</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/70615</guid><dc:creator><![CDATA[Terry R]]></dc:creator><pubDate>Mon, 18 Oct 2021 20:04:57 GMT</pubDate></item></channel></rss>