Regex: Insert a new line in files, only if that line not exist in files
-
@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