Notepad++ is a nifty tool that includes a regex search-and-replace feature. Regex (regular expressions) are extremely powerful when it comes to string modification.
The Basic Regular Expression Operators
Some of the operators date back to old typewriters, but all in all it’s not so difficult to understand:
- $ – end of string
- ^ – beginning/start of string
- \ – escape character
- . – any character except line break
- + – Must match at least 1 times
- * – Matches an empty string or multiple. Basically a wildcard operator
- \n – line break (LF/Line Feed, next line, not moving cursor)
- \r – carriage return (move to first position in line, also known as CR)
- \n\r – entire blank line (carriage return + line break)
When you combine operators like . and + your last operator will tell the preceding operator how to match a string. Example:
.* (match any character 0 or more times)
.+ (match any character 1 or more times)
Replacing Empty Lines In Notepad++
1. Step Open notepad++ – you should have something like this with empty lines:
2. Step Now hit CTRL + F and go to the tab Replace. At the bottom check the option Regular Expression and enter \n\r into the search field. Leave the “replace with” field blank:
3. Step When you hit Replace All, it should replace all empty lines in your document
Wait! It says 0 occurrences, what am I doing wrong?
4. Step One common mistake notepad++ beginners make is that they forget that notepad++ basically behaves like a sophisticated typewriter. You first need to tell notepad++ to move the cursor to the first line, first position. My cursor currently is at the very end, so when I say Replace All, it says “0 occurrences”.
Adding character to beginning or end of line
Another common operation you will encounter every other day is adding characters before or behind a line that you copied from Excel. Excel makes it very easy to do this, but in notepad++ you have to use regular expressions or use the key combo ALT + F to mark multiple rows. However, the ALT + F + mouse selection key combo does not work so well for adding characters after each line, so you’re better off learning regex.
First, let’s add something before each line:
Next, we are adding something to the end:
See, wasn’t that hard, was it?
If you keep forgetting the operators ^ and $ you should print out the regex sheet cheat I linked above and pin it to your board and one glance is sufficient to remind you of regular expressions. This is also a good technique to learn other operators, but only frequent use of the operators will help you to remember them indefinitely.
Replacing Special Characters Using RegEx
Special character manipulation requires you to make use of escape characters. It’s very easy to forget to escape special characters properly. Characters such as “?”, “.”, “$” have special meaning (as explained above) so in order to replace them using regular expression you will have to escape them using a backslash “\” before the character.
So, let’s go one step further and try to match any character followed after a question mark:
The full sequence to match any character:
In the replacement field you don’t have to escape the character, simply write it out like your normally would and it will replace the part that matches (.*)
Still have questions? Post below.
Why Learn Regular Expressions
All sorts of programs use regular expressions. Apache uses it for its mod_rewrite module. PHP functions such as preg_match make use of regular expressions and many search tools support it.
In a nutshell, if you ever want to program anything, regular expression will help you to become more productive and perform simple or complex string modifications.
Trending On Windows Themes .Net