Community
    • Login

    Regex: Insert a new line in files, only if that line not exist in files

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    16 Posts 4 Posters 894 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Terry RT
      Terry R
      last edited by Terry R

      @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 the space before content. 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 RT 1 Reply Last reply Reply Quote 0
      • Hellena CrainicuH
        Hellena Crainicu
        last edited by

        @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.

        1 Reply Last reply Reply Quote 0
        • Terry RT
          Terry R @Terry R
          last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • Hellena CrainicuH
            Hellena Crainicu
            last edited by

            @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…

            1 Reply Last reply Reply Quote 0
            • Terry RT
              Terry R
              last edited by Terry R

              @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

              1 Reply Last reply Reply Quote 1
              • guy038G
                guy038
                last edited by guy038

                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 description

                Then, 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 part My 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 the Replace 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

                1 Reply Last reply Reply Quote 1
                • Hellena CrainicuH
                  Hellena Crainicu
                  last edited by

                  @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.

                  alt text

                  Terry RT 1 Reply Last reply Reply Quote 0
                  • Terry RT
                    Terry R @Hellena Crainicu
                    last edited by

                    @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

                    1 Reply Last reply Reply Quote 0
                    • Hellena CrainicuH
                      Hellena Crainicu
                      last edited by

                      @Terry-R of course I press Replace All button

                      Terry RT 1 Reply Last reply Reply Quote 0
                      • Robin CruiseR
                        Robin Cruise
                        last edited by

                        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

                        1 Reply Last reply Reply Quote 1
                        • Terry RT
                          Terry R @Hellena Crainicu
                          last edited by

                          @Hellena-Crainicu
                          Your screen print shows the Find Next button as being active, that is why I asked.

                          Terry

                          1 Reply Last reply Reply Quote 0
                          • guy038G
                            guy038
                            last edited by

                            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 file

                            BR

                            guy038

                            1 Reply Last reply Reply Quote 2
                            • Hellena CrainicuH
                              Hellena Crainicu
                              last edited by

                              thanks @guy038 and @Robin-Cruise

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post
                              The Community of users of the Notepad++ text editor.
                              Powered by NodeBB | Contributors