Community
    • Login

    How to add a missing line, based on part of the previous line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    13 Posts 2 Posters 416 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.
    • guy038G
      guy038
      last edited by guy038

      Hello, @dr-ramaanand and All,

      @dr-ramaanand, I’ve finally managed to understand what your goal was ! Not so easy !


      You said :

      However, I want to know how to use a $1 after the \r\n instead of typing the whole (missing) string to be added

      Well, no need to build a regex which uses Backtracking Control verbs for this task and, anyway, you’re probably not yet able to master these concepts !

      So, the obvious solution is to use the following regex S/R :

      • SEARCH <footer(.+?class=).+?>

      • REPLACE $0\r\n<div$1"container">

      • Check the Wrap around option

      Thus, from this INPUT text, pasted in a new tab :

      <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
      

      After a click on the Replace All button, you would get the expected OUTPUT text :

      <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
      <div style="width: 100%; height: auto;" class="container">
      

      Now, there’s still a problem with that S/R : if you repeat the Replace All action, you would wrongly get :

      <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
      <div style="width: 100%; height: auto;" class="container">
      <div style="width: 100%; height: auto;" class="container">
      

      On the other hand, you also said :

      I would also love to know how to use a ?! which is a negative look ahead, before the missing string to add it using a $1 in the Replace field

      To prevent this case to occur and, as you suggested, the solution is to verify that the line <div style="width: 100%; height: auto;" class= does not exsist, right after the line <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">

      This leads to the final regex S/R, which uses, as you said, a negative look-ahead, containing a reference to our group 1

      • SEARCH <footer(.+?class=).+?>(?!\R<div\1)

      • REPLACE $0\r\n<div$1"container">

      This time, after a first click on the Replace All button, any subsequent click on this button will display, as 'expected, the message :

      Replace all: 0 occurrence were replaced in entire file

      This ensures that the replacement just occurs once !


      Remark :

      • In the search regex, you may replace the \1 syntax, within the look-ahead structure, by any of the eight following syntaxes :

      \g1 , \g{1} , \g<1> , \g'1' , \k1 , \k{1} , \k<1> or \k'1'

      • Howewer, you CANNOT use the $1 or ${1} syntaxes in the search regex. These two syntaxes and, of course, \1, are the three syntaxes ONLY allowed in the Replace regex !

      To summarize, this regex S/R adds the line <div style="width: 100%; height: auto;" class="container">, whose part space + style="width: 100%; height: auto;" class= is also a part of the previous line !

      Thus, I decided to rename the title of this topic as : How to add a line, based on part of the previous line

      Best Regards,

      guy038

      dr ramaanandD 1 Reply Last reply Reply Quote 0
      • dr ramaanandD
        dr ramaanand @guy038
        last edited by

        @guy038 merci beaucoup but, “How to add a line, based on part of the previous line if that line is missing” is a more appropriate title

        dr ramaanandD 1 Reply Last reply Reply Quote 0
        • dr ramaanandD
          dr ramaanand @dr ramaanand
          last edited by dr ramaanand

          @guy038 One of my files out of several in a folder had this footer: <footer style="top: 98.7%; width: 100%; height: auto;" class="panel-footer footer add-top"> and another had <footer style="top: 99.2%; width: 100%; height: auto;" class="panel-footer footer add-top">, so I tweaked your Regular expression to <footer style=".+?(width: 100%; height: auto;" class=).+?>(?!\R<div\1) in the Find field but then, it does not skip adding the <div style="width: 100%; height: auto;" class="container"> if it is already added in those two files. I also observe that <footer style="width: 100%; height: auto;" class="panel-footer footer add-top"> cannot be found even if there is no <div..............> on the immediate next line. Does your Regular expression need more tweaking? <footer style=".+?(width: 100%; height: auto;" class=)+?>(?!\R<div style="\1) does not find/match anything

          dr ramaanandD 1 Reply Last reply Reply Quote 0
          • dr ramaanandD
            dr ramaanand @dr ramaanand
            last edited by

            I tweaked the Regular expression to <footer style="[^"]*?(width: 100%; height: auto;" class=).+?>(?!\s*<div style="\1) with $0\r\n<div style="$1"container"> in the replace field which worked and it also skipped adding the <div…> line if it was added already.

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

              Hi, @dr-ramaanand,

              Sorry, but I did not understand, at all, what you want to !


              If I paste in a new tab, these two lines, as INPUT :

              <footer style="top: 98.7%; width: 100%; height: auto;" class="panel-footer footer add-top">
              
              <footer style="top: 99.2%; width: 100%; height: auto;" class="panel-footer footer add-top">
              

              my previous regex S/R :

              • SEARCH <footer(.+?class=).+?>(?!\R<div\1)

              • REPLACE $0\r\n<div$1"container">

              Does return this OUTPUT text :

              <footer style="top: 98.7%; width: 100%; height: auto;" class="panel-footer footer add-top">
              <div style="top: 98.7%; width: 100%; height: auto;" class="container">
              
              <footer style="top: 99.2%; width: 100%; height: auto;" class="panel-footer footer add-top">
              <div style="top: 99.2%; width: 100%; height: auto;" class="container">
              

              You’ll note that the added line, beginning with <div style=", is followed, as expected, with the same text that its previous line, except for the text which comes after the class attribute and which is changed as container

              Do you need this behaviour ? If not, show me some examples ( before and after ) of what you need !


              Now, you said :

              I also observe that <footer style="width: 100%; height: auto;" class="panel-footer footer add-top"> cannot be found even if …

              But, althougth the part top: ##.#%; is missing, my regex <footer(.+?class=).+?>(?!\R<div\1) does find the string

              <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
              

              Thus, from the INPUT text :

              <footer style="top: 98.7%; width: 100%; height: auto;" class="panel-footer footer add-top">
              
              <footer style="top: 99.2%; width: 100%; height: auto;" class="panel-footer footer add-top">
              
              <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
              

              and the regex S/R :

              • SEARCH <footer(.+?class=).+?>(?!\R<div\1)

              • REPLACE $0\r\n<div$1"container">

              We get the following OUTPUT text :

              <footer style="top: 98.7%; width: 100%; height: auto;" class="panel-footer footer add-top">
              <div style="top: 98.7%; width: 100%; height: auto;" class="container">
              
              <footer style="top: 99.2%; width: 100%; height: auto;" class="panel-footer footer add-top">
              <div style="top: 99.2%; width: 100%; height: auto;" class="container">
              
              <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
              <div style="width: 100%; height: auto;" class="container">
              

              Again, if these results are NOT expected, make the effort to provide a lot of examples, which CLEARLY show what you want to !

              BR

              guy038

              dr ramaanandD 1 Reply Last reply Reply Quote 0
              • dr ramaanandD
                dr ramaanand @guy038
                last edited by

                @guy038 said in How to add a missing line, based on part of the previous line:

                <footer style=“top: 98.7%; width: 100%; height: auto;” class=“panel-footer footer add-top”>

                <footer style=“top: 99.2%; width: 100%; height: auto;” class=“panel-footer footer add-top”>

                <footer style=“width: 100%; height: auto;” class=“panel-footer footer add-top”>

                If the above is the input text, with your RegEx, the output is exactly as you showed above but I want the output in all three cases to be <div style="width: 100%; height: auto;" class="container">

                dr ramaanandD 1 Reply Last reply Reply Quote 1
                • dr ramaanandD
                  dr ramaanand @dr ramaanand
                  last edited by dr ramaanand

                  @guy038 …so I tweaked the Regular expression to be used in the Find field to <footer style="[^"]*?(width: 100%; height: auto;" class=).+?>(?!\s*<div style="\1) with $0\r\n<div style="$1"container"> in the replace field which added the <div.........> line and it also skipped adding the <div,…> line if it was added already.

                  dr ramaanandD 1 Reply Last reply Reply Quote 1
                  • dr ramaanandD
                    dr ramaanand @dr ramaanand
                    last edited by dr ramaanand

                    @guy038 The very first solution I had provided, right on top was actually perfect but I thought the <div.............> string could be captured in a group. However, someone at regex101.com explained that SKIP is just an optimization to say, “don’t just move 1 character and try again”, it says, “move ALL these characters and try again”, so if something is skipped with a (*FAIL) or (*F), we can’t capture it in a group

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

                      Hello, @dr-ramaanand and All,

                      Ah…, I’ve just tested your S/R and it works nicely !

                      Now, you can even simplify your S/R to this version :

                      • SEARCH <footer.+?(width.+?class=).+?>(?!\R<div style="\1)

                      • REPLACE $0\r\n<div style="$1"container">


                      Now, if, in your very first post, you had said :

                      From this INPUT text :

                      <footer style="top: 98.7%; width: 100%; height: auto;" class="panel-footer footer add-top">
                      
                      <footer style="top: 99.2%; width: 100%; height: auto;" class="panel-footer footer add-top">
                      
                      <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
                      

                      I would like this OUTPUT text :

                      <footer style="top: 98.7%; width: 100%; height: auto;" class="panel-footer footer add-top">
                      <div style="width: 100%; height: auto;" class="container">
                      
                      <footer style="top: 99.2%; width: 100%; height: auto;" class="panel-footer footer add-top">
                      <div style="width: 100%; height: auto;" class="container">
                      
                      <footer style="width: 100%; height: auto;" class="panel-footer footer add-top">
                      <div style="width: 100%; height: auto;" class="container">
                      

                      Everything would have been much more simple, you know !?

                      Best Regards,

                      guy038

                      dr ramaanandD 1 Reply Last reply Reply Quote 1
                      • dr ramaanandD
                        dr ramaanand @guy038
                        last edited by

                        @guy038 Oui, merci beaucoup!

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