Community
    • Login

    How to stop searching or replacing after a string?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    regex
    24 Posts 5 Posters 2.3k 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,

      I said :

      Here is the minimal syntax of the generic S/R …

      However, I received a message from @terry-R, by chat, who, seemingly have found out a shorter syntax that mine which looks as the true minimal possible syntax ;-))


      @terry-r, from my previous solution :

      • SEARCH (?-i:<h2|(?!\A)\G)(?s-i:(?!</h2).)*?\K(?-i:/p>\R<p)

      • REPLACE /h2>\r\n<h2

      I could have slightly changed this S/R by :

      • SEARCH (?-i:<h2>|(?!\A)\G)(?s:(?!<).)*?\K(?-i:</p>(\R)<p)

      • REPLACE </h2>$1<h2>

      Which, in turn, can be expressed as :

      • SEARCH (?-i:<h2>|(?!\A)\G)[^<]+\K(?-i:</p>(\R)<p)

      • REPLACE </h2>$1<h2>

      If we assume that there is a single < char near the very end of each line ( remember that, AFTER the replacement, the regex position is RIGHT AFTER the string <p> )


      Note that this new solution is quite closed to your solution, exposed in your chat, if we do not use non-capturing groups nor the the -i modifier to ensure the search of valid HTML tags :

      • SEARCH (<h2>|\G)[^<]+\K</p>(\R)<p>

      • REPLACE </h2>${2}<h2>

      How, this new formulation works with my example ?

      SEARCH   (?x) (?-i: <h2> | (?!\A) \G )    [^<]+    \K  (?-i: </p> ( \R ) <p> )
                          ----                  -----              ---------------
                          BSR                 ESR = '<'                  FR
      
      
      First, as we start at VERY BEGINNING of file, the regex looks for a FIRST '<h2>' string
      
      
      <h2>The licenses for most software are designed to take away your freedom to share and change it.  </p>
      <h2>-----------------------------------------[^<]+-----------------------------------------------\K</p>CRLF
      
      <p>  By contrast, the GNU General Public License is intended to guarantee your freedom to share  </p>
      <p>\G-----------------------------------------[^<]+--------------------------------------------\K</p>CRLF
      
      <p>  and change free software--to make sure the software is free for all its users.  </p>
      <p>\G-----------------------------------------[^<]+--------------------------------\K</p>CRLF
      
      <p>  This General Public License applies to most of the Free Software Foundation's software  </p>
      <p>\G--------------------------------------------------------------------------------------\K</p>CRLF
      
      <p> and to any other program whose authors commit to using it. </h2>
      <p>
      
          ( On THIS line, impossible to find the string '</p>CRLF<p>', near the END of line, so the NEXT \G syntax is NOT true anymore
            Thus, NO MORE replacement occurs till a NEW '<h2>' string happens and the NEXT THREE lines remained UNTOUCHED ! )
      
      <p>(Some other Free Software Foundation software is covered by the GNU Library General</p>
      <p>Public License instead) You can apply it to your programs, too.</p>
      <p>When we speak of free software, we are referring to freedom, not price.</p>
      
      <h2>Our General Public Licenses are designed to make sure that you have the freedom to distribute  </p>
      <h2>-----------------------------------------[^<]+-----------------------------------------------\K</p>CRLF...
      
      On this LAST line, the RE-SYNCHRONIZATION occurs because a '<h2>' string is found and the cycle RESTARTS !
      

      Best Regards,

      guy038

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

        @guy038 there is only one, “<h2>” and one, “</h2>” in the file, so what @PeterJones helped me, “guess”, based on what you typed in an earlier thread seems to be good enough

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

          @PeterJones using “(?-si:<h2|(?!\A)\G)(?s-i:(?!<\/h2).)*?\K(?-si:<\/p>\R<p[^<>]*>)” in the “Find what” field and “</h2>\r\n<h2>” in the “Replace All” field is working perfectly. Thank you very much. Happy New Year!

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

            @PeterJones @guy038 I am sorry to say that I tested this on my laptop just now as I was busy with the New Year celebrations and observed that the RegEx which Peter Jones told me to use is also replacing only one </p> and one <p.............. > on the next line instead of replacing all at once.

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

              @dr-ramaanand said in How to stop searching or replacing after a string?:

              @PeterJones @guy038 I am sorry to say that I tested this on my laptop just now as I was busy with the New Year celebrations and observed that the RegEx which Peter Jones told me to use is also replacing only one </p> and one <p.............. > on the next line instead of replacing all at once.

              I’m sorry to say that my experience is different than yours. Replace All with the regex I suggested works all at once.

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

                @PeterJones it looks like you took a long time to reply, so I asked at www.regex101.com and was told to use, “(?-si:<h2[^<>]*+>|\G)[^<>]*+\K<\/p>\s*+<p[^<>]*+>” which is replacing all that I want replaced, all at once!

                dr ramaanandD PeterJonesP 3 Replies Last reply Reply Quote 0
                • dr ramaanandD
                  dr ramaanand @dr ramaanand
                  last edited by dr ramaanand

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • PeterJonesP
                    PeterJones @dr ramaanand
                    last edited by PeterJones

                    @dr-ramaanand ,

                    It was less than an hour. if that’s a “long time” in your mind, then using a free, Community-based service is probably not the right question/answer format for you. I would suggest finding someone to pay to give you instant 24/7/365.2425 support. Because you aren’t going to find anything guaranteed faster in any free online Q&A site.

                    And as we’ve told you before, regex101 uses a different flavor of regex engine than Notepad++ does, so there is no guarantee that a regex suggested by that site will be compatible with Notepad++. Use at your own risk.

                    Further, as you have reported multiple times, and as my video showed, the regex shown does work (as you once said, “perfectly”), and you didn’t need my video to know that. If you want to use a different regex, that’s fine, go ahead and use whatever “works” for you. But don’t pretend (and publically state) that it was a defficiency in the regex already given to you or a “slow response” from me or any of the other regulars here who answer questions out of the kindness of our hearts.

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

                      @PeterJones I am sorry if it offended you. I didn’t mean to.

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

                        @dr-ramaanand said in How to stop searching or replacing after a string?:

                        (?-si:<h2[^<>]*+>|\G)[^<>]*+\K<\/p>\s*+<p[^<>]*+>

                        @guy038 or anybody else, can you please explain what the above RegEx does (since it is working on Notepad++)? It replaces all the </p> and the <p............ > immediately after that, even if it’s on the next line, all at once with </h2> and <h2> on the immediate next line, if I put </h2>\r\n<h2> in the “Replace with” field and hit “Replace All” with the Regular expression mode ticked and the matches newline unticked. The best thing about it is it does not do any replacing after the last </h2> !

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

                          @dr-ramaanand I think the people on regex101 are using ChatGPT or other artificial intelligence applications to generate regular expressions.

                          The expressions you get from regex101 are extremely difficult to follow do not come with any explanation of how the logic works or why certain choices were made. It’s likely that the regex101 people also have little to no idea of what the expressions do.

                          That’s a large part of why using ChatGPT or other artificial intelligence applications is banned on this forum. Ideally, we call can learn from the answers we see posted on this forum.

                          As I posted earlier, it would be much better for you to so stick with things you know and understand well. It’s ok to push the limits a little at times as you can learn from those. Trying to make sense of what looks like AI generated content is a waste of time.

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