Computer Programming

Notepad++ Regular Expressions Rules And Samples

Several different types of Regular Expressions, or short Regex = Regular Expressions, exist in different applications. Notepad++ uses PCRE (Perl Compatible Regular Expressions) type of Regex. Notepadpp (Notepad++) uses slightly different syntax.

Along with simple editing tasks Notepad++ contains Regular Expressions plugins. In fact it has two such plugins, for simple and more advanced tasks. Simple operations can be done using simple Search and Replace. For example if we need to replace o word or several word. But Notepad++ has other options. They are all combined in one dialog. The dialog is evoked by pressing Ctrl+F or Ctrl+H shorcuts or through Main Menu Search -> Find or Search -> Replace.

Simple Search and Replace is called Normal. We can see it at the top of 3 bottom left lines in the dialog. If it’s not enough then more advanced Extended Search and Replace helps in many cases. For example it helps when line breaks should be replaced also. If one needs still more advanced program, he can use Regular Expressions. To employ even more sophisticated algorithm the 4th plugin is available at Main Menu TextFX -> TextFX Quick -> Find/Replace. It can be called with shortcut Ctrl+R. That is the second, more developed Regular Expression sensitive function. It includes multiple line search and replace.

Here is all 4 Search and Replace functions in Notepad++:

Simple Search and Replace called Normal

Extended Search and Replace using backslash \

Regular Expressions Search and Replace

TextFX Find/Replace with Regular Expressions including multiple lines Search and Replace

It should be mentioned that Regular Expressions are also a part of PHP programming language. Most programmers start learing them sooner or later. We can find libraries for C++ facilitating their use. And finally, for performing the most complicated tasks, better than abovementioned Regex plugins can be only a special program in C++, PHP of any other computer language. Among other tasks they are really helpful in performing C++ programming routine tasks, replacing certain expressions in huge pieces of code.

Every now and then we need to perform repetitive routine tasks like replacing text and lines of test. Some of them can be done through simple Search and Replace dialog. More sophisticated require using Extended function and Regular Expressions.

Notepad++ Regular Expression Regex Rules – loading

Notepad++ Regular Expression Regex Rules

Here is the set of rules used in Notepad++ Regular Expressions:

. – any character

* – any number of repetitions of the preceding character, 0 or more reps

+ – one or more repetitions of the preceding character, 1 or more reps

? – 0 or 1 occurrence of the preceding character

\. \? \* \+ means . ? * + accordingly

[] – will match of any characters placed inside square brackets. For example [x] will match x, xx, xxx etc

() – grouping

In replace \1, \2 etc are replaced with first, second etc groupings

^ – start of line

$ – end of line

\d – single digit

\w – single alphanumeric character

\s – whitespaces, which is spaces, tabs, line breaks

\< – start of word

\> – end of word

Notepad Regular Expressions samples:

\r\n\r\n – removes all blank lines (both in Extended search and Regex Search and Replace)

^First.*$ matches all lines beginning with the word First

^.*s$ matches all lines with the letter s at the end

^\? Test.*$ matches all lines beginning with “? Test” till the end of line

Leave a Reply

Your email address will not be published. Required fields are marked *