Community
    • Login

    regex: find two different strings on two consecutive lines

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 1.8k Views 1 Watching
    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.
    • Vasile CarausV Offline
      Vasile Caraus
      last edited by Vasile Caraus

      hello. I have this kind of strings, on two consecutive lines. Before and after string, on every line, should be empty:

           <p>&nbsp;</p>  
          <p class="text_obisnuit">
      

      So, I need to find all those 2 consecutive lines, and replace it with:

           <br><br>  
          <p class="text_obisnuit">
      

      it would have been a simple search and replace if those strings had been positions in the same place, but may be disturbed by spaces before and after. This is why I need a regex.

      My solution, is not too good :(

      SEARCH: \S\s(?s)(<p>&nbsp;</p>)\S\s|\n\r|\S\s(?s)(<p class="text_obisnuit">)\S\s
      REPLACE BY: <br><br>\n\2

      Eko palypseE 1 Reply Last reply Reply Quote 0
      • Eko palypseE Offline
        Eko palypse @Vasile Caraus
        last edited by

        @Vasile-Caraus

        so am I correct to assume that you want to change the tags AND
        delete the spaces as well?
        If so, what about this (\h*<p>&nbsp;</p>)\h*\R\h*(<p class="text_obisnuit">) ?

        1 Reply Last reply Reply Quote 3
        • Vasile CarausV Offline
          Vasile Caraus
          last edited by

          yes, Eko, your regex is great !! I write it here again with the replace:

          SEARCH: (\h*<p>&nbsp;</p>)\h*\R\h*(<p class="text_obisnuit">)
          REPLACE BY: <br><br>\n\2

          Thank you very much

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

            Hello, @vasile-caraus, @eko-palypse and All,

            Here is, below, a similar regex S/R, with a look-ahead structure, which, also, preserves the indentation of the two lines, involved !

            So assuming the sample text, below :

                <p>&nbsp;</p>  
                <p class="text_obisnuit">
            
                    <p>&nbsp;</p>  
                        <p class="text_OTHER_1">
            
            <p>&nbsp;</p>  
                  <p class="text_obisnuit">
            
              <p>&nbsp;</p>  
            <p class="text_OTHER_2">
            
                    <p>&nbsp;</p>  
                <p class="text_obisnuit">
            

            If we use :

            SEARCH (\h*)<p>&nbsp;</p>(?=\h*\R\h*<p class="text_obisnuit">)

            REPLACE \1<br><br>

            the above text becomes :

                <br><br>  
                <p class="text_obisnuit">
            
                    <p>&nbsp;</p>  
                        <p class="text_OTHER_1">
            
            <br><br>  
                  <p class="text_obisnuit">
            
              <p>&nbsp;</p>  
            <p class="text_OTHER_2">
            
                    <br><br>  
                <p class="text_obisnuit">
            

            With the previous regex S/R, we would have obtained :

            <br><br>
            <p class="text_obisnuit">
            
                    <p>&nbsp;</p>  
                        <p class="text_OTHER_1">
            
            <br><br>
            <p class="text_obisnuit">
            
              <p>&nbsp;</p>  
            <p class="text_OTHER_2">
            
            <br><br>
            <p class="text_obisnuit">
            

            Note, also, that this new regex S/R works, whatever the line endings ( \r\n for me and \n for you )

            Best regards,

            guy038

            1 Reply Last reply Reply Quote 4

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors