Friday, January 26, 2007

Why you should learn programming: use of regex

Suppose your boss gives you a list of names, given below, and asks you to remove the numbers. Hapless you have no other way than to go to each line and delete the number. If you have little idea of programming, you would never think that way. You will immediately try to find the least time consuming way. You can write a program to do the job for you. I won't recommend that. But if there are hunderds of such files that need to be sorted out, then you should definitely embrace programming. However for the task given below, there's another tool that will really make your life a happy life. Regular expressions. Sometimes even the good programmer are not aware of such tools. These tools are also available in almost all high level languages.

Most text editors also come with regular expression functionality. Oh don't menion about useless Ms Notepad. I use notepad++, however there are plenty of other free editors lying around the net.

It just takes a second to extract the names from the list below. I mean a second for me and some of you. Because I already know a bit of regular expression. It's a really confusing at the beginning and you don't have to be a programmer to understand regular expression, though programming idea definitely helps.

I am assuming you are not using notepad. If you are, then change it right away.

1 FirstA LastA
2 FirstB LastB
3 FirstC LastC
100 FirstAA LastAA
1000 FirstAAA LastAAA
1001 FirstAAB LastAAB

The list goes on

The presentation below shows some regular expressions to manipulate the list of 1000 rows, but definitely not a comprehensive one to master regular expressions. Just know that there's also regular expression around the corner, should you need to do some work expeditiously.

No comments: