How to Use Regular Expressions in Notepad++
Regular expressions are a powerful text search and replace tool that can help you quickly find text matching specific patterns. Notepad++ has built-in support for regular expressions, allowing you to perform complex search and replace operations. This tutorial will detail how to use regular expressions in Notepad++ for matching and replacing text, helping you process text more efficiently.
What are Regular Expressions?
Regular Expressions (RegEx) are special text strings used to describe string matching patterns. They can be used to search, replace, and extract text that matches specific patterns, making them a very powerful tool for text processing.
Advantages of Regular Expressions: Using regular expressions, you can match multiple text patterns with a single expression, without writing complex conditional statements. For example, you can use one expression to match all email addresses, phone numbers, or dates in a specific format.
Enabling Regular Expression Search in Notepad++
Notepad++'s search and replace functionality supports regular expression patterns. Here are the steps to enable regular expression search:
- 1Open Notepad++ and load the file you need to process.
- 2Press Ctrl + F to open the "Find" dialog, or press Ctrl + H to open the "Replace" dialog.
- 3In the dialog, check the "Regular expression" option.
- 4Enter your regular expression in the "Find what" box.
- 5For replace operations, enter the replacement text in the "Replace with" box.
- 6Click "Find Next", "Find All", "Replace", or "Replace All" to execute the operation.

Basic Regular Expression Syntax in Notepad++
Here are the commonly used regular expression metacharacters and syntax in Notepad++:
Basic Metacharacters
.
- Matches any single character (except newline)*
- Matches the preceding expression 0 or more times+
- Matches the preceding expression 1 or more times?
- Matches the preceding expression 0 or 1 time^
- Matches the start of a line$
- Matches the end of a line\
- Escape character, used to match special characters literally|
- OR operator, matches either expression on either side of |
Character Classes
[abc]
- Matches any single character in the brackets[^abc]
- Matches any single character not in the brackets[a-z]
- Matches any lowercase letter from a to z[A-Z]
- Matches any uppercase letter from A to Z[0-9]
- Matches any digit from 0 to 9
Predefined Character Classes
\d
- Matches any digit, equivalent to [0-9]\D
- Matches any non-digit, equivalent to [^0-9]\w
- Matches any letter, digit, or underscore, equivalent to [a-zA-Z0-9_]\W
- Matches any non-letter, non-digit, non-underscore, equivalent to [^a-zA-Z0-9_]\s
- Matches any whitespace character (space, tab, newline, etc.)\S
- Matches any non-whitespace character
Quantifiers
{n}
- Matches exactly n times{n,}
- Matches at least n times{n,m}
- Matches between n and m times
Groups and References
(pattern)
- Captures the matched subexpression, can be referenced in replacement using \1, \2, etc.(?:pattern)
- Non-capturing group, cannot be referenced in replacement
Tip: In Notepad++, you can use \1, \2, etc. in the replacement text to reference captured groups from the regular expression. For example, if your regular expression is (abc)(def)
, then \1 refers to "abc" and \2 refers to "def".
Practical Regular Expression Examples
Here are some commonly used regular expression examples in Notepad++:
Example 1: Find All Email Addresses
Regular Expression: \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b
This expression can match most common email address formats.
Example 2: Find and Replace HTML Tags
Find: <([a-zA-Z][a-zA-Z0-9]*)[^>]*>(.*?)</\1>
Replace: [\1]\2[/\1]
This expression can replace HTML tags with a custom format. For example, <div>Hello</div>
will be replaced with [div]Hello[/div]
.

Example 3: Remove Leading Whitespace
Find: ^\s+
Replace: Leave empty
This expression can remove spaces, tabs, and other whitespace characters at the beginning of each line.
Example 4: Convert CamelCase to snake_case
Find: ([a-z])([A-Z])
Replace: \1_\2
This expression can convert camelCase naming to snake_case naming (e.g., "camelCase" becomes "camel_Case").
Advanced Tips: Using Regular Expressions for Batch Operations
Multiline Mode
In Notepad++, regular expressions are in single-line mode by default. If you need to process text across multiple lines, you can use special syntax:
(?m)
- Enable multiline mode, making ^ and $ match the start and end of each line(?s)
- Enable single-line mode, making . match all characters including newlines
For example, (?m)^#.*$
can match all lines starting with #.
Using Regular Expressions to Remove Empty Lines
Find: ^\s*$\r?\n
Replace: Leave empty
This expression can remove all empty lines from the document (including lines containing only whitespace).

Using Regular Expressions to Extract Specific Content
For example, to extract all content between double quotes:
Find: "([^"]*)"
Replace: \1
This expression can replace "Hello World"
with Hello World
, removing the double quotes.
Frequently Asked Questions
Question 1: My regular expression isn't working?
If your regular expression isn't working, it might be because:
- Forgot to check the "Regular expression" option.
- Regular expression syntax error, check for unmatched parentheses, brackets, etc.
- Special characters not properly escaped, such as ., *, +, ?, ^, $, \, | which need to be escaped with \.
- Using regular expression features not supported by Notepad++.
Question 2: How to use capture groups in replacement?
In the replacement text, you can use \1, \2, \3, etc. to reference captured groups from the regular expression:
- Use parentheses () in the find expression to capture the parts you want to keep.
- Use \1 to reference the first capture group, \2 for the second, and so on in the replacement text.
- For example, finding
(Mr\.) (Smith)
and replacing with\2, \1
will replace "Mr. Smith" with "Smith, Mr."
Question 3: How to match special characters literally?
To match special characters literally (such as ., *, +, etc.), you need to escape them with a backslash \:
- To match a period, use
\.
- To match an asterisk, use
\*
- To match a plus sign, use
\+
- To match a question mark, use
\?
- To match a backslash itself, use
\\
Summary
Regular expressions are a very powerful text processing tool in Notepad++, and mastering them can greatly improve your text editing efficiency. Through this tutorial, you have learned how to enable regular expression search in Notepad++, learned basic regular expression syntax, and through examples, understood how to apply regular expressions to solve practical problems.
Remember, learning regular expressions is a gradual process. Start with simple patterns and gradually try more complex expressions. As you gain experience, you will be able to write more precise and efficient regular expressions.
We hope this tutorial helps you become proficient in using regular expressions for text processing in Notepad++!