Find and Replace with Regular Expressions in Notepad++: A sitemap.xml Example

1 min read
Regular expressions are invaluable when you need to inspect a large file, find specific patterns and replace or remove them.

For example, suppose you need to remove every <lastmod> entry from a sitemap.xml file containing more than 9,000 lines. Finding and deleting all of them manually would be impractical.

Notepad++ and Regular Expressions


You can use Notepad++ to complete this task:

1. Open sitemap.xml using Notepad++.
2. Open Search → Replace (or press Ctrl+H).
3. In the ‘Replace’window, under Search Mode, select ‘Regular expression’.
4. In the ‘Find what’ field, enter the following regular expression: <lastmod>.*?</lastmod>\R.
5. Make sure the ‘Replace with’ field contains no characters. It must be completely empty.
6. Click ‘Replace All’.

If the steps were followed correctly, the program will find and replace all matching lines.

How the Regular Expression Works


Using the pattern <lastmod>.*?</lastmod>, the program finds lines that start with and end with . Any characters may appear between those tags.

Using \R removes the blank lines left after deleting the tags and their contents.

Please note:

Before modifying any file, sitemap.xml in this example, make a backup by saving a copy under a different name. If anything goes wrong while deleting lines, you will still have the original file.
Back to blog