Search code examples
regexnotepad++textpad

How do I replace all of the text between two phrases with regular expressions in either Notepad++ or Textpad?


I have about 50 html documents and I need to replace the text between

<!DOCTYPE

and

<!-- start content -->

with

<?php require("header.php"); ?>

From what I've read, Notepad++ does not support multiple line regular expressions but I thought I might put that in the question too. I'm new to regular expressions so I need someone to tell me how to do this. Thanks in advance!


Solution

  • I have neither Notepad++ nor Textpad installed, so I can't say for sure, but one of the following might work there:

    Search for

    1. <!DOCTYPE[\s\S]*?<!-- start content --> or
    2. <!DOCTYPE.*?<!-- start content --> with the "Dot matches newline" option set or
    3. (?s)<!DOCTYPE.*?<!-- start content -->

    and replace that with <?php require("header.php"); ?>.

    This will remove everything between the two phrases (including the phrases themselves). If you don't want that (I'm not sure from your question), then you probably want to keep what's on the first line after <!DOCTYPE, right? So:

    Search for (<!DOCTYPE[^\r\n]*)[\s\S]*?<!-- start content --> (or (?s)(<!DOCTYPE[^\r\n]*).*?<!-- start content --> etc.),

    and replace with $1<?php require("header.php"); ?><!-- start content -->