Notepad++ Regular Expression Find and Replace

When scripting or manipulating text data I prefer using notepad++. It’s easy to use, includes syntax coloring for just about anything, folds code nicely and even supports regular expression find and replace features. I have been using if for years but just recently have come to love the program even more. When using find and replace in the past I stuck mostly to the basics. From time to time I would use a regex to search and then replacing everything that was found. Notepad++ goes well past this. To explain I will show an example. I was working with tabular data that looked something like this:

603,1znsk71,
554,3yc7r61,
228,9qzyh11,
478,fs77r41,
423,7t77r41,
455,6wlj251,
pc0481,hrj6l11,
562,jcbrt61,
348,5lx1g41,
376,69r9l51,

The data was a list of our internal PCs and their serial number. The problem was that this format was not ready to be inserted into our database. Rather than scripting the fix I figured notepad++ would do the trick. First, I knew that the correct format should be something like this: 0### or pc0###. Fortunately all the the “PC” prefixes were intact but the number only entries were not (that data was collected into an Excel file with cells set to the “number” format instead of “text”). I drafted a regular expression that would find only what I was looking for. It turned out like this:

^([0-9]+),

This regex found everything just fine. The problem came when I wanted to do the replacement. I am really a regex noob having only used them a few times in PHP, Perl and Apache mod_rewrite so this was all new to me. I know mod_rewrite used “$1” to print what was found be the prior regex. I tried with no luck in notepad++. After some searching online I finally found the answer! The replacement result looked something like this:

pc0\1, 

After doing a find and replace on everything my dataset turned out like this:

pc0603,1znsk71,
pc0554,3yc7r61,
pc0228,9qzyh11,
pc0478,fs77r41,
pc0423,7t77r41,
pc0455,6wlj251,
pc0481,hrj6l11,
pc0562,jcbrt61,
pc0348,5lx1g41,
pc0376,69r9l51,

I hope you found this useful!

This entry was posted in Other Stuff and tagged . Bookmark the permalink.

Comments are closed.