Regular Expressions

Regular Expressions (commonly referred to as RegEx or RegExp) are coded strings which represent text patterns. These can be used to search and replace patterns in the texts. A pattern defined by a regular expression can be used in the regular expression processors like grep, sed, awk, Notepad++, Visual Studio etc. for performing search, replace or modifications.

Programming languages also support regular expressions. It is more commonly found in scripting languages like Perl, Python, Ruby etc. but the compiled languages Java and C++ (using Boost, Qt) also support it.

Regular Expressions Syntax and Rules

A regular expression is a string and contains text characters. A few of the text characters have special meaning. These special characters perform various operations like grouping, quantification, NOT etc. Rest of the characters are normal and mean what they are.

List of special characters:  ” \  [  ]  ^ –  ?  .  *  +  |  (  )  $  /  {  }  %  <  >

To use these characters literally in a regex one has to escape them using a backslash (\) or enclose in quotes.

Normal Text

  • a matches a
  • hello matches hello
  • . a special character. It matches any character except a newline
  • \. matches .


Round brackets or parentheses () are used to create a group.

  • (a|b|c) matches a or b or c
  • (colour|color) matches colour or color


Square brackets [] are used for specifying a range of characters.

  • [abc] matches a or b or c
  • [^abc] matches any character except a, b and c
  • [a-c] matches a or b or c, range from a to c
  • [A-C] matches A or B or C, range from A to C
  • [a-cA-C] matches a or b or c or A or B or C
  • [0-5] matches a digit from 0 to 5
  • [a-zA-Z0-9] matches one character of alphanumeric text


It specifies how many times something is repeated.

  • a* zero or more, matches null, a, aa, aaa, aaaa, …
  • a+ one or more, matches a, aa, aaa, aaaa, …
  • a? zero or one, matches null or a
  • a{3} matches aaa
  • a{3,} three and more aaa, aaaa, aaaaa, …
  • a{3,6} 3 to 6 matches aaa, aaaa, aaaaa, aaaaaa

[a-zA-Z0-9]{8} matches alphanumeric text of length 8


Shorthands exist for commonly used character classes. For example digits character class has a shorthand \d. It is short for [0-9]. Each lowercase shorthand character has an associated uppercase shorthand character with the opposite meaning. Thus [\D] matches any character that is not a digit, and is equivalent to [^\d].

  • \d a digit, short for [0-9]
  • \D a non-digit, short for [^0-9]
  • \w a word character, short for [a-zA-Z_0-9]
  • \W a non-word character equivalent to [^a-zA-Z_0-9]
  • \s a whitespace character, short for [ \t\n\x0b\r\f] matches any whitespace character. This includes spaces, tabs, and line breaks.
  • \S [^\s]


  • ^ start of the subject, ^a makes sure that a occurs at the the beginning of the subject
  • $ end of the subject, a$ makes sure that a occurs at the the end of the subject
  • \b word boundaries, \ba\b makes sure that a is a whole word in the subject
  • \B nonboundaries, \Ba\B makes sure that a is not a whole word


  • .at matches any three-character string ending with at e.g. hat, cat and bat
  • [hc]at matches hat or cat
  • [hc]?at matches hat or cat or at
  • 0[xX][A-Fa-f0-9]+ matches hexadecimal number e.g. 0x2AB7
  • [A-Za-z_][A-Za-z_0-9]* identifier in a programming language
  • cal[ae]nd[ae]r matches misspelled calender
  • colou?r matches color or colour
  • gr[ae]u matches grey or gray
  • \d+ matches positive integers
  • -\d+ matches negative integers
  • -{0,1}\d+ matches integers
  • \d*\.{0,1}\d+ matches positive real numbers

Prof. Walter Lewin’s Video Lectures on Physics

Prof. Walter Lewin is a Professor Emeritus of Physics at the MIT. He is famous for the interesting physics video lectures freely available on the internet. These lectures has been used by more than four million students and teachers all over the world. Walter Lewin Lectures on Physics takes you through classical mechanics, electricity and magnetism and vibration and waves.

Prof. Lewin says:

If the student says I find physics boring and dull it’s simply means one thing that they had a bad teacher. Any good teacher can turn physics into something absolutely spectacular.

Prof. Lewin takes these lectures to a whole new level. He has prepared around 600 physics demonstration for 100 lectures. He presents the concepts along with the its live demonstrations. Demonstrations are very important, students enjoy it and help them to learn easily.

A lot of hard work and preparations goes into delivering the single one hour lecture. For every lecture he gives takes about 40 hours to prepare. He rehearses three times in real-time with an empty class and writes on board whatever he has to write during the the lecture. Giving a dry run of the same lecture three times is really hard. He also has to take all sort of measures to have safe demonstrations. He would try his best to make experiments interesting, he would hang to a pendulum and explain that time period is independent of mass. At times, he would put his life on the line by demonstrating his faith in the Conservation of Mechanical Energy.

I have enjoyed watching these lecture. I first came to know about these in early 2006 during my fourth year at IIT Kanpur. I thank my friend Deepak Sachan for introducing these great lectures to me.

I suggest do watch these lectures if you are a physics enthusiast. These are definitely beneficial for high school and undergraduate students. I definitely recommend these video lectures for IIT-JEE preparation. Every physics teacher should also watch these.

Link to the video lectures on MIT OpenCourseWare:

Here is a short interview with Prof. Lewin:

Logitech Wireless Desktop MK 250 Review

logitech-wireless-desktopIt has been getting difficult to work for long hours on laptop’s keyboard. So I decided to buy a wireless keyboard and mouse for my laptop.

After searching a little over net I found Logitech Wireless Desktop MK250 a very good option. I could easily find it in the market also. It cost me Rs 1150 in Delhi.

I found it very easy to setup. It was literally plug and play. Just hook the USB receiver in laptop and right away start using the keyboard and mouse. No driver installation or any kind of configuration is required. I tried it on Windows XP and Ubuntu.

The package contains:

  • Keyboard
  • Mouse
  • USB receiver
  • 1 AA and 2 AAA batteries (pre-installed in the device)
  • Troubleshooting card

I was surprised to find the batteries also :)

Here is my take on it:


  • Very good battery life: 15 months of keyboard and 6 months for mouse
  • Great range, they claim 10 meter and I confirm 5 meter (~15 feet). Therefore it is suitable for HTPC also.
  • Great value for money.
  • No setup or configuration required. Truly plug-n-play!
  • Works with Linux. I tried on Ubuntu.


  • The placement of Home and End keys is different from the earlier one I used.
  • No support of Logitech Unifying Receiver.
  • The size of USB receiver is big. If it could have been made like the Logitech Unifying Receiver one could just plug it and forget. With the big size, I have to remove the USB receiver while carrying the laptop in bag.

Rooting Android

Lately, I rooted my Android device. My main purpose was to tether phone-internet to my laptop. All the the tethering Apps required rooted kernel. I had no choice but to root my phone.

A little googling led me to a very nice and step-by-step guide for rooting Samsung Galaxy Spica i5700. Following the same I was able to successfully root my phone without any glitches.

Rooting will not format your phone. Everything will remain intact, your files, Apps installed, Apps data, SD card data etc. The idea behind rooting is that kernel is changed to a modified one. The modified kernel enables to run certain Apps with the root permissions and let your phone do stuff which requires super user permissions.

The image, LK2.08, which I used contains the modified kernel and a couple of Apps, e.g., SuperuserApp and Wireless Tether. LK2.08 is built by LeshaK from the modified kernel sources of Samsung Spica. You can look at the modification in the GitHub repository.

I will try understand the kernel modifications, see the internals and technical details of rooting and post something in future.

How URL Shortening Works

Daily, we come across a number of short URLs while surfing web. A short URL redirects to another URL (usually a long URL). For example, the URL can be shortened to using The short URL will just redirect to the long one.

There are many URL Shortening services available, e.g.,, tinyurl and the latest one by Google. Have you ever wondered how the URL shortening works? This article will take you through the concepts behind URL shortening. I will touch upon only the high level details and will not discuss any of the implementation stuff.

Understanding Short URL

If you look at a short URL, it consists of a domain and a key:
Domain =
Key =

The key is different for each long URL. Based on the key the server redirects to the respective long URL.

How does it really works?

Let us take as the domain of the service provider for explaining purpose. Here is equivalent of,, etc.

The service provider,, maintains a map of keys and URLs in the database. Where key is a positive integer and URL is just a string. The map looks like:

  • [0, url0]
  • [1, url1]
  • [2, url2]

Where url0, url1, url2… are the long URLs. The integer is unique and always incremented when a new short URL is created. It can be made sure before creating a new short URL, if a long URL already exists in the database then don’t add it again and use the same short key.

On opening the short URL= in the browser, the request goes to the server with the key. The server checks the key in the map and redirects to respective long URL. For example:

  • -> url0
  • -> url1
  • -> url2

This is one of the techniques which can be used to implement a simple URL Shortening service.

I don’t see keys as alpha-numeric

Usually, we don’t see integers as the keys. All the services use alpha-numeric string as the key. The reason behind this is that on can accommodate more keys without increasing the length of key.


For a key length of 6: base10 can have 10^6 keys and base 62 can have 62^6 keys.

Using base62 in the above mentioned technique is not difficult to implement. Keep the same database. Store integers itself in the database. The server will get requests as key in base62 and you will have to convert base62 key to base10 before finding the corresponding long URL in the database and then redirect to the long URL.

Introduction to App Inventor for Android

App Inventor for Android is a tool for creating applications for Android platform. But it is different. It doesn’t require you to write code to develop an application.

Usually, application development involves programming and in case of Android one has to program in Java. This tool, App Inventor, on the other hand doesn’t require you to program. Using this tool you can create applications for Android without writing a line of code!

How can an app be created without writing the code? The answer lies in another question: What does the code do in an app?

The code does two things:

  1. Defining components i.e. defining things like widget, Button, Label, TinyDB, screen arrangement, notifications etc. Components can also be called objects or instances.
  2. Defining and controlling the behavior of these defined components.

App Inventor achieves the above two objectives without any programming. In App Inventor you can define the components and their behavior visually through a user interface.

App Inventor has a Designer and a Blocks Editor. The Designer defines all sorts of components and the Blocks Editor defines the behavior of these components.

Let me quickly take you through the Setup Process and the Development Environment of App Inventor.

Setup Process

The App Inventor is a web-based tool and works on all major browsers. Therefore the setup is very minimal. Following are the basic requirements for running App Inventor:

  • Contemporary operating systems and browsers.
  • Java Run Time Environment 1.6 (i.e. Java 6).
  • App Inventor Extras Software: Installer/Package is available on the home page of App Inventor. One can easily download and install it.
  • USB Connectivity with USB Debugging enabled: The setup requires you to have USB Debugging enabled in your phone and a working USB connectivity. To achieve the same you need to install drivers for your phone. The Extras Software package includes drivers of a few common Android devices and after installing the same you might not need to install the drivers manually. If this doesn’t work, you can manually install the drivers by using the CD you got along with your phone.

I found the setup pretty easy. I was able to get App Inventor working very quickly.

After completing the setup you can quickly test your setup. Create an empty project and then clicking the Connect to Phone from the Blocks Editor should launch the stub application on your phone.

Development Environment

It has two parts Designer and Blocks Editor. Designer defines the look and feel of your application. You can drag and drop the objects (buttons, textBox, media, sensors etc) from the palette to the screen and arrange them as desired. All this is done visually.

App Inventor Designer Window


After completing the design you can open the Blocks Editor from the Designer. In the Blocks Editor window you can define the behavior of the components which you added previously. In this window, you will have various types of Blocks associated with the components e.g. OrientationChanged-block of OrientationSensor-component, the block where you can do call your other blocks to get the notification when the orientation changes.

App Inventor Blocks Editor

This was a quick introduction of App Inventor. And in the next part of this post I will explain how to create a simple application for Android using the App Inventor.

Debugging Console for GuiClient/QXmpp

QXmpp repository contains many examples. One of them, GuiClient, is a full-fledged graphical XMPP client. I developed it for testing out the functionality and usability of the QXmpp API. This example supports most of the XEPs (XMPP Extension Protocols) supported by QXmpp.

To ease up the debugging, I have added a console dialog to display all the sent/received stanzas. Have a look at the Debugging Console.


Debugging Console


  • Settings button can be used to launch this dialog.
  • User can control the logging by using the Enable Check Box.
  • Sent and Received messages are color coded to differentiate between these.
  • The Clear button clears the text area.

Got Invitation of App Inventor for Android

Yesterday, I got the invitation to try the pre-release of App Inventor for Android. App Inventor is a tool which allows users without any programming skills to create applications for Android platform. Instead of writing the code, one has to visually design the user interface and create blocks to control the behavior of the application.

I will try it out and create something using the App Inventor. Wait for my next post for the review or may be a tutorial.

More about App Inventor from the team:

Welcome to App Inventor!

About App Inventor:
App Inventor for Android allows people with minimal programming experience to create simple, personal apps for Android devices. It has a number of features which ease app development. App Inventor is best suited for people who are eager to learn the basics of programming and are interested in making basic apps for their personal use.
Remember the Beta Tag:
App Inventor is currently available as an invitation-based beta product. We are limiting access in order to ensure that our systems can handle the load. As a beta product App Inventor still has rough edges and missing features. In some cases the rough edges include un-pleasantries such as: installing Windows device drivers, installing Java on your computer, and fiddling with settings on your phone. We are working hard to smooth out the rough edges and we appreciate your use of App Inventor while we are in this beta state. It may sound a bit cliche but it really is true, your use of App Inventor today will help us make it better for the future! Now on to the good stuff.
Getting Started with App Inventor:

Complete these 3 steps to start using App Inventor:
  1. Set up your phone and computer.
  2. Connect your phone to your computer.
  3. Complete the basic tutorials.
Take your App Inventor knowledge to the next level by:
Happy Inventing!
Google’s App Inventor Team

Implementing XOR Encryption

At times, you need a simple encryption and decryption functionality to secure sensitive information. In my case, I needed to store passwords on disk to implement the “Remember Password” functionality of the GuiClient example of QXmpp.

Instead of going for a 3rd party library where you just need a very basic crypto functionality I will suggest using XOR encryption. XOR encryption is pretty easy to implement. I will also present my implementation of this algorithm using Qt C++.

XOR (Exclusive OR) Encryption or XOR Cipher is a simple symmetric encryption algorithm. It operates according the principle that XORing a data twice with the same key results in the same data.

The first XOR of the data and key gives the encrypted data. Then the decryption involves XORing the encrypted data with the same key.

EncryptedData  = Data ^ Key
Data =  EncryptedData ^ Key

Data: data to be encrypted
Key: secret key or password
EncryptedData: data after encryption
^ represents Exclusive-OR (XOR) operation


Let us use two binary numbers for Data and Key. The 

Data  = 01101
Key   = 10101


EncryptedData = Data ^ Key
EncryptedData = 01101 ^ 10101
EncryptedData = 11000


Data = EncryptedData ^ Key
Data = 11000 ^ 10101
Data = 01101

Implementation of XOR Encryption in Qt C++:

QByteArray calculateXor(const QByteArray& data, const QByteArray& key)
   return data;

 QByteArray result;
 for(int i = 0 , j = 0; i < data.length(); ++i , ++j)
   if(j == key.length())
     j = 0;// repeat the key if key.length() < data.length()
   result.append( ^;
 return result;