Notepad++ - Replace with Regular Expression

> Integrated development environment (ide) > NotePad++

1 - About

Replace with regular expression in Notepad++.

Notepad++ uses the Boost Library (C++). For the whole regex pattern format, you may have to refer to it.

3 - Tutorial

3.1 - The Input text

Input with windows end of line (ie \r\n)

Hello          Print Hello
Youhou         Print Youhou

You can see the end of line when checking the “View all characters” options (View > Show Symbol > Show all characters or the reverse P icon)

Advertising

3.2 - The Search Mode options

where:

  • regular expression means that you input is a regular expression
  • . matches newline if the checkbox is checked means that the search will not occurs for each line but for the whole document when the dot is used.

3.3 - Regular Expression

([\w]*)([\s]*)(.*)(\r\n){0,1}

which means:

  • takes all alphabetical characters (\w) until you found something that is not a alphabetical character.
  • takes all space until you found something that is not a space (the \s class means any whitespace character)
  • takes all characters . until you found the end of line \r\n or nothing {0,1} (ie the end of file)

Don't check the check box ( . matches newline )

3.4 - Replace

Replace with

| \1 | \3 |\4

where:

  • \1 correspond to the match of the first pair of parenthesis,
  • \2 to the second and so on.

The fully qualified expression for substitution is

${n}

and must be used when:

  • the number of group is greater than 9
  • you want a number that follow the substitution
Advertising

3.5 - Output

Output after hitting the replace button:

| Hello | Print Hello |
| Youhou | Print Youhou |

4 - Snippets

4.1 - Take all letters until a separator character is reached

This is achieve with the negation character ''^'' of a class

Example with the - minus. Take all characters until a minus is found:

  • Find what:
([^-]*).*
  • Replace with:
\1

Example of input:

libX11-1.6.5
lksctp-tools-1.0.17
mailcap-2.1.41

gives this output:

libX11
lksctp
mailcap
Advertising

4.2 - Replace a character with new line

4.3 - Suppress blank line

5 - Documentation / Reference

ide/notepad/replace.txt · Last modified: 2019/11/06 20:14 by gerardnico