Community
    • Login

    Notepad++ delete 2 lines below a specific string

    Scheduled Pinned Locked Moved General Discussion
    10 Posts 6 Posters 1.2k 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.
    • F
      Foodland2023
      last edited by

      I want to delete 2 lines from a long text where system finds a specific string. As an example, if it finds string “Supplier” then only the next 2 lines should be deleted and not the line having Supplier…
      in the below example, the highlighted lines must be removed, and this should happen recursively throughout the file.
      24044970-4d53-4a07-9bea-8addd2232615-image.png

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

        @Foodland2023 ,

        given data

        Supplier: ABCD
        deleteme
        andme
        keepme
        keepme
        Supplier: 1234
        deleteme
        andme
        keepme
        keepme
        keepme
        keepme
        

        FIND = (?-s)(Supplier.*)(\R.*){2}
        REPLACE = $1
        SEARCH MODE = Regular Expression
        REPLACE ALL
        results in

        Supplier: ABCD
        keepme
        keepme
        Supplier: 1234
        keepme
        keepme
        keepme
        keepme
        

        … which is what I believe you want.

        Quick explanation:

        • Regular Expression mode allows logic in the search-and-replace
        • (?-s) = makes sure that .* will stay on a single line, and not match across lines
        • (Supplier.*) = matches the word Supplier plus everything else to the end of the line, and puts it in group#1
        • (\R.*) = matches a newline sequence plus everything on that line
        • {2} = matches the previous thing twice; in this case, it will make it match two lines and the newlines that come before them
        • Replacement of $1 replaces with the contents of group#1, but nothing else from the match, effectively deleting the two lines after

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • Formatting Forum Posts
        • Notepad++ Online User Manual: Searching/Regex
        • FAQ: Where to find other regular expressions (regex) documentation
        F 1 Reply Last reply Reply Quote 3
        • F
          Foodland2023 @PeterJones
          last edited by

          @PeterJones said in Notepad++ delete 2 lines below a specific string:

          (?-s)(Supplier.)(\R.){2}

          @PeterJones - Awesome man, thanks a lot for quick turnaround… it worked as I was expecting…

          F wat wat 0W 2 Replies Last reply Reply Quote 0
          • F
            Foodland2023 @Foodland2023
            last edited by Foodland2023

            @PeterJones- I am pretty new to using Search options, i am getting the explanation provided but still not getting the core concept of using these expressions \R\N\n {} $ while searching the text.
            Could you suggest any link which gives the detailed explanation of all these?
            Once again, thanks for your timely help!

            CoisesC PeterJonesP 2 Replies Last reply Reply Quote 1
            • CoisesC
              Coises @Foodland2023
              last edited by

              @Foodland2023 said in Notepad++ delete 2 lines below a specific string:

              detailed explanation

              https://npp-user-manual.org/docs/searching/#regular-expressions

              https://www.regular-expressions.info/

              At the second website, know that Notepad++ uses the Boost regular expression library, so when differences between regular expression implementations are discussed, Boost is the relevant one.

              F 1 Reply Last reply Reply Quote 4
              • F
                Foodland2023 @Coises
                last edited by

                @Coises - Thanks a lot, this helps for me to get started!

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

                  @Foodland2023 said in Notepad++ delete 2 lines below a specific string:

                  Could you suggest any link which gives the detailed explanation of all these?

                  Yes. In fact, I already did. In my post above: the USEFUL REFERNCES in big bold letters led to links to the Notepad++ Online User Manual: Searching/Regex and to the “FAQ: Where to find other regular expressions (regex) documentation”. That rather gave you what you asked for two hours before you asked for it.

                  1 Reply Last reply Reply Quote 2
                  • wat wat 0W
                    wat wat 0 @Foodland2023
                    last edited by

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • DIGITAL MARKETINGD
                      DIGITAL MARKETING
                      last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote -1
                      • Sarah SmithS
                        Sarah Smith
                        last edited by

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