Community
    • Login

    How to multiline regex find + replace a block of code that varies across multiple files

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 28 Views 2 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.
    • M Offline
      MayTheCatgirl
      last edited by

      Hi all,

      I’ve been having trouble trying do use the Notepad++ find and replace in a very specific way.

      To explain my situation, I’m archiving an old blog, and have hundreds of HTML files for it. Most of these files use the blog’s newer formatting which is missing some content on the sidebar. I also have a copy of one of the blog pages from 2008 that has the formatting I’d like to use.

      So essentially I want to do a regex search to find the block of code to replace (which varies file-to-file and is only consistent in the code it begins and ends with), and replace it with the code from the older site. I’ve got this working manually, but I obviously don’t want to copy paste the code in one at a time for 300+ files.

      I tried using the following expression with “. matches newline” enabled and it finds the code block:
      [beginning of code].+?[end of code]
      However Notepad++'s find and replace does not support multiline find and replace, and when I use it this way to paste the 100-line block of code where I want it, it messes up the formatting and cuts off the last few lines.

      Per a different thread, I installed the ToolBucket plugin as it has multiline find and replace functionality. However, the ToolBucket find and replace will not search across multiple lines when using the expression above.

      I’d love to just use the default Notepad++ one for this as its “find in files” feature is a lot more helpful than requiring me to open all 300 files I want to search through but it seems that is not an option.

      Is there perhaps a different expression I could use with ToolBucket to find the beginning and end of the code block and select it and everything in between? Or a different program or tool entirely that would suit my use case better?

      Thanks :)

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

        @MayTheCatgirl ,

        I don’t know the ToolBucket plugin itself – I don’t use it – but if it doesn’t have the . matches newline checkbox like Notepad++'s native dialog does, you might be able to use (?s)[beginning of code].+?[end of code], where the (?s) is the regex flag for “enable dot matches newline”, to force the same behavior (but it depends on the specifics of what regex engine ToolBucket uses)

        1 Reply Last reply Reply Quote 0

        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