How to multiline regex find + replace a block of code that varies across multiple files
-
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 :)
-
I don’t know the ToolBucket plugin itself – I don’t use it – but if it doesn’t have the
. matches newlinecheckbox 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) -
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 replaceI’m wondering what you mean by “does not support multiline find and replace.” I think you must mean that you can’t easily enter a replace expression that contains multiple lines.
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.
There are a couple possible problems. I believe there is a character limit (I don’t recall what it is) to how much you can put in the Replace box.
But also… are you considering that you can’t just blindly copy a block of code into that box, because some characters have special meanings? In particular, backslashes, parentheses, and depending on the context, dollar signs, question marks and colons, have special meanings and must be escaped.
I assume you are substituting the original content, so you’re using something like:
Find: OLDPREFIX(.*?)OLDSUFFIX
Replace: NEWPREFIX$1NEWSUFFIXto do it? Or is the replacement content exactly the same every time, using no part of the original content?
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.
It can probably be done. It might have to be done in a couple steps instead of just one, if the replacement expression has too many characters to put in the box all at once.
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