## Friday, 24 May 2013

### Notepad++ Find and Replace

I think Notepad++ is a great program. I use it a lot at work and at home. I have used it to process text in various ways. I really like Notepad++'s find and replace options, especially since the Regular Expression engine was updated to PCRE. A great post on Notepad++'s find and replace can be found on Mark's Speechblog. In the examples below, don't type in the double quotes. Here are a few Find and Replace searches I have found useful:

This one turns new lines into commas with spaces. I use it to convert a column of Excel data into text as a comma separated list. I've since written an AutoHotkey script to do this too (because I do this so often), which will be the topic of a future post.
Search Mode: Extended
Find: "\r\n"
Replace with: ", "

This finds a specific second column of freeform text from several other columns with digits. The second find string converts a line like "11937577    M0609/VD06   108721      11933802    198024      11933835" into just "M0609/VD06".
Search Mode: Regular expression
Find: "\d+ +(.+)" and "\d+ +(.+) +\d+ +\d+ +\d+ +\d+"
Replace with: "\1"

This one uses start and end of line anchors to ensure the whole line is matched, and grabs the last column. It converts something like "108608     Left turn MVT from Road SEB to Ave NEB    NULL       9      7774/SEB/LQ" into "7774/SEB/LQ".
Search Mode: Regular expression
Find: "^.* +(.+)$" Replace with: "\1" This one puts a capital M in front of lines that begin with a digit. Search Mode: Regular expression Find: "^(\d)" Replace with: "M\1" This one splits some text in a specific tabular form into separate lines. Search Mode: Regular expression Find: "(\d+/[^\t\r\n]*\t[^\t\r\n]*\t[^\t\r\n]*\t(SB|NB)\t[^\t\r\n]*\t[^\t\r\n]*\t[^\t\r\n]*\t[^\t\r\n]*\t[^\t\r\n]*\t)" Replace with: "\1\r\n" This one removes a tab from the end of a line if it has any. I should have put the last \t in the above regular expression outside of the group, as in "(...[^\t\r\n]*)\t". Search Mode: Regular expression Find: "\t$"
Replace with: "" (nothing)

For this find and replace, I wanted to wrap everything on non-empty lines with some other text (in this case it's for a custom SQL query).
Search Mode: Regular expression
Find: "^(.+)\$"
Replace with: "   or v.Identifier like '%\1%'"

Have fun with Notepad++!