Regular Expressions in Notepad++

I’ve always found regular expressions to be a real un-intuitive mind-bender. I’ve had some very limited success in using them…mostly in a linux command-line grepping situation where I had some very good examples to work from and had no other choice…i.e. I was FORCED to use them. I’m a programmer. I’m a database administrator. I enjoy coding and writing weird SQL! But I’ve never been able to get my head around regular expressions…never really wanted to. See my blog post about my laziness with regular expressions. 😉

I’ve been using Notepad++ for some time now and have sort of a love-dislike relationship with it because it’s very powerful…but some things, notably macros don’t exactly work like I’d want them too…and sometimes, I really think they’re buggy.

Today, I needed to take a list of e-mails and, for each email take the part before the ‘@’ and copy it at the of the line with a tab in between.

So, to start with I have a list of emails like this:

lkjshdflkhsf [at] lkjhsdf [dot] com
oiuyeruyq [at] iuywer [dot] com
nbxvnxbv [at] mnbcx [dot] com

At the end I need lines like this:

lkjshdflkhsf [at] lkjhsdf [dot] com[TAB]lkjshdflkhsf
oiuyeruyq [at] iuywer [dot] com[TAB]oiuyeruyq
nbxvnxbv [at] mnbcx [dot] com[TAB]nbxvnxbv

If you’ve ever had to move contacts from one piece of software to another you may have come across this problem to create an import file for the destination. If Notepad++ macros worked in every situation, I’d have been able to use them, but they would simply “malfunction” on me and not allow me to accomplish this. So I played with regular expressions and became aware of how you can “tag” parts of your search expression and then refer to those tagged portions of text in your replacement string to allow you to move things around and copy things. Really good regex info can be found on the Notepad++ wiki here: http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Regular_Expressions.

So to accomplish this I was able to use the Notepad++ Search/Replace dialog in “regular expression” mode like this:

notepad_regex_example.png

In the image, you can see that in the FIND expression, you have two sections in parentheses. This is how you tag a portion of a string, by putting the expression within parentheses. For the first piece, it tags the part between the beginning of the line, signified by the “^” (caret) character and the “@”, signified by itself. The “dot asterisk” means:

  • dot = any character
  • asterisk = zero or more times

I’ve shaded that yellow. Then the second section that I’ve tagged is between the “@” and the end of the line, signified by the “$” (dollar sign). I’ve shaded it orange.

Now, in the replacement, you can refer to these tagged areas by “1”, “2”, “3”, etc….simply the order in which they appear gives them an ordinal number. You can then refer to them in the replacement string several times if you wish.

I’m still quite the noob in regular expressions and much more can be done with them. But, I found this particular example to be something that I can definitely build on in the future and hope that you can too.

RegEx Nirvana?

My technology path has covered a fairly large swath of operating systems and user environments. I’ve gotten quite familiar with different line-command, GUI and full-screen text (3270) environments. I typically customize any environment I work with.

Currently as part of our missionary work, I’m a systems/network administrator in a small private school in France. I’m the only fish in the pond so I get to do everything. It also means I have to do everything. I don’t have the luxury, therefore, to get good at every technology that I work with. If I don’t know it, I have to teach myself, etc. No training budget here.

My background in programming in various environments and database administration in various environments serves me well in many situations. On thing continues to give me fits, however, regular expressions. These beasts are anything but regular! One thing they are, however, is ubiquitous! As a “mostly Windows guy”, I could get by without them, but even there, more and more pieces of software are adding regex capabilities into their find/replace functions. Add to that the fact that I maintain Linux servers (a self-taught area that has caused me no small amount of head-scratching!) and I absolutely cannot escape regular expressions.

As stated earlier, I don’t have the time to learn every good piece of technology that’s out there, nor a training budget…which translates to no tool budget either. So, I need a good piece of freeware/open source that “holds my hand” in the occasional, but deep, regular expression activity. There’s lots of freeware out there, but 90% of it is simply testing regex…not really helping you understand how to write them. I would search, in vain, occasionally for something that could help me.

Well, I finally found something!!!!

Regulazy (and its big brother Regulator) are written by Roy Osherove and can be found here: http://weblogs.asp.net/rosherove/pages/tools-and-frameworks-by-roy-osherove.aspx I highly recommend them as ideal tools to help you build and test regex’s.

I resent the name Regulazy, but I love its facility to write for you the regex by stepping through a complex string and suggesting appropriate matching expressions for each part of it.

Anyway, thanks Roy for those great tools!!!! You’re helping busy sysadmins the world over!

Technorati : , , , , , ,
Del.icio.us : , , , , , ,