Regex: Insert a new line in files, only if that line not exist in files
-
In almost all my hml files, I have this 2 lines:
<meta property="og:title" content="My Title"/>
<meta property="og:description" content="My description of article"/>
But I made a search with regex, to find all files that does not contain the second line:
SEARCH:
(?s)\A(?!.*(meta property="og:description).*$)
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.
So, I need to find only those files that contain only the first line, and add to all this the second line
So, I try this solution, but does not working:
SEARCH:
(?s)\A(?!.*(meta property="og:description).*$)
REPLACE BY:
\1
-
@Hellena-Crainicu said in Regex: Insert a new line in files, only if that line not exist in files:
So, I need to find only those files that contain only the first line, and add to all this the second line
So, I try this solution, but does not working:
SEARCH: (?s)\A(?!.(meta property="og:description).$)
REPLACE BY: \1When you have text within the
()
it allows you to capture itIF
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\1
with the actual text to insert.Terry
-
@Terry-R I also try this solution of yours. Doesn’t work… :(
-
@Hellena-Crainicu said in Regex: Insert a new line in files, only if that line not exist in files:
I also try this solution of yours. Doesn’t work… :(
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 thespace
beforecontent
. Without that it’s difficult to establish exactly what the regex needs to look like.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 (
\1
) then in the replacement field have\1(text here to be inserted)
or something similar such as using\K
which is an advanced regex function.Terry
-
@Terry-R very close answer. So, I try to pur an
\r\n
SEARCH:
(?s)\A(?!.(meta property="og:description).$)
REPLACE BY:\1\r\1(<meta property="og:description" content="My description of article"/>)
but this will add the line before, not after the first line.
-
@Terry-R said in Regex: Insert a new line in files, only if that line not exist in files:
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.
I’ll repeat my request. To insert examples, copy them into the posting area, select and then hit the
</>
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.Without examples it will be difficult for anyone to help you.
Terry
-
@Terry-R said in Regex: Insert a new line in files, only if that line not exist in files:
I’ll repeat my request. To insert examples, copy them into the posting area, select and then hit the </> 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.
Without examples it will be difficult for anyone to help you.I don’t understand…
-
@Hellena-Crainicu said in Regex: Insert a new line in files, only if that line not exist in files:
I don’t understand…
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 here. It also references another post on how to format, which is what you need to read next.
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.
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.
Terry
-
Hello, @hellena-crainicu, @terry-r and All,
If your line
<meta property="og:description"••••••••••
follows immediately the line<meta property="og:title"••••••••••/>
, a possible solution
would be :SEARCH
(?-is)\A<meta property="og:title".+\R\K(?!<meta property="og:description")
REPLACE
<meta property="og:description" content="My description of article"/>\r\n
The SEARCH regex will match a zero length string, right after the first line
<meta property="og:title" content="My Title"/>
, whatever its title, ONLY IF it is not followed with a second line<meta property="og:description" content="My description of article"/>
, whatever its descriptionThen, the REPLACE regex simply inserts, at this empty location, a complete line
<meta property="og:description" content="My description of article"/>
followed with the usual Windows line-break chars\r\n
( or use\n
only, if you deal with UNIX files )
So, the road map is :
-
Open the Replace dialog (
Ctrl + H
) -
SEARCH
(?-is)\A<meta property="og:title".+\R\K(?!<meta property="og:description")
-
REPLACE
<meta property="og:description" content="My description of article"/>\r\n
Replace the partMy description of article
by the real text, of course ! -
Tick the
Wrap around
option, only ( IMPORTANT ) -
Select the
Regular expression
search mode -
Click on the
Replace All
button ( Do not use theReplace
button ! )
Of course, test the matches and the replacements, produced by this regex S/R, on one or few files, before applying it on a large bunch of files ;-))
Best Regards
guy038
-
-
@guy038 said in Regex: Insert a new line in files, only if that line not exist in files:
(?-is)\A<meta property=“og:title”.+\R\K(?!<meta property=“og:description”)
something suspicios, @guy038 See this print screen please. I try your both regex formulas.
-
@Hellena-Crainicu said in Regex: Insert a new line in files, only if that line not exist in files:
something suspicios, @guy038 See this print screen please. I try your both regex formulas.
Did you press the
Replace All
button as requested. As the regex uses the\K
function clicking on any other option may cause the issue you see.Terry
-
@Terry-R of course I press
Replace All
button -
Another simple solution, by two steps:
1. Add the line you want after the one with
og:title
in all your files:SEARCH:
(<meta property="og:title" content=".*"/>)
REPLACE BY:
\1\r<meta property="og:description" content="My description of article"/>
2. Delete all duplicate lines
SEARCH:
(?-s)(^.*\R)\1+
REPLACE BY:
\1
-
@Hellena-Crainicu
Your screen print shows the Find Next button as being active, that is why I asked.Terry
-
Hi, @hellena-crainicu, @terry-r and All,
Ah…OK. But, as your first regex attempt contains the
\A
assertion, relative to the very beginning of file, I wrongly supposed that the line<meta property="og:title" content="My Title"/>
was always the first line of the file :-(And, from your screen-shot, the line
<meta property="og:title" content="My Title"/>
may also be the last line of current file, even without any line-ending char !!
So here is a new version which covers all cases possible :
SEARCH
(?-is)^<meta property="og:title".+(?:\K(\z)|\R\K(?!<meta property="og:description"))
REPLACE
(?1\r\n)<meta property="og:description" content="My description of article"/>\r\n
So, each time that the regex engine meets the string
<meta property="og:title"
, with this exact case, beginning a line, it inserts, right after, the new line<meta property="og:description" content="My description of article"/>
, with this exact case + its line-break chars, but ONLY IF not already followed with a line beginning with<meta property="og:description"
OR IF it is located at the very end of fileBR
guy038
-
thanks @guy038 and @Robin-Cruise