Notepad++ Regular Expression Usage

Did you know that you can use regular expressions in Notepad++? Well, it sure comes in handy sometimes.

If you don’t know what a regular expression is; here is a quick intro for you:

A regular expression (regex for short) is a special text string for describing a search pattern.

We, as software developers, use it a LOT! Many software applications and programming  languages support regular expressions. You can often accomplish with a single regular expression in one or a few lines of code what would otherwise take dozens or hundreds.

However, it is also used quite often in daily life as well. To exemplify this, think of a website,(actually this is what I had to use it for just now, so this is why I am giving this example) which has many many links. And you want to get the location of these links.(Assume for the sake of this example that the text of those links will not contain the link locations)

Since there are a LOT, right clicking and selecting ‘copy link address’ will not work for this type of situation(unless you are VERY patient).Thus, our little friends come to our rescue. You select only the places with anchor links from the HTML source code, and copy it into your favorite text editor(or Notepad++ here) and then you write a quick regular expression that reflects the fact that a link is comprised of a starting and ending tag, and some attributes.

In my case the anchor tag was something like this :

<a href="the link address">File 1</a>

and all I wanted was the link locations. (Of course first select ‘Regular expression’ as the search mode.) Since we need it quick and dirty without any thinking at all(:p) the following regex would do the job: <a href="(.*)">(.*)</a>

So we could write this into the "Find what:" box in Notepad++, and in the corresponding "Replace with:" box we could for example write \2 \1 and get the link title along with its location. Note the use of parantheses here( e.g in (.*) ) , they form up a group which allows us to directly infer to them in our replace regex by saying the 2nd group, nth group etc.

Use;

[0-9]* to select numbers only

[a-zA-Z]* to select alphabetic characters

.* to match any character

[^"]* to match any character that is not a quote(of course, you can change the quote into any other character) [so; the first ^ in a [ ] denotes negation of the contents of that particular [ ] ]

\n to match endline character

\t to match tab character

and so on.. This should get you going.. If you need more complex regular expressions or wanna learn about it, just Google it, there are millions of resources out there on the Internet.

About these ads

4 Comments

  1. ashish said,

    February 22, 2009 at 9:01 am

    Thanks

  2. 1 said,

    February 2, 2010 at 10:05 am

    Thanks

  3. anjella said,

    April 14, 2011 at 12:36 am

    Awesome, thanks!

  4. Jen said,

    October 19, 2011 at 7:16 pm

    thanks a lot…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: