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: https://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.

Archives

Categories

Important links

leighweb.com – our family web site
surleslinteaux.leighweb.com – my wife’s French Sunday School blog
eglisejosue.fr – our church in France
tdr-guebwiller.eu – our house of prayer (HOP) in France
informatique.leighweb.com – My web development freelance business
My CV/Resume in English (PDF)