Thursday, August 13, 2009

[SW] Regexp substitution in VIM

Vim is my favorite text editor. I use vim for editing plain text or programming. It has many features which I've not been familiar with, and the substitution with regexp is on of them.

I had a little problem in one of my script file, which has been used to chop some data files into shorter segments for further usage. When editing the script, I need to delete some parts which are in the form of ``some digits, some digits''.

For example, I need to delete ``90,185'' or ``1455,2010'' in the script for composing a newer one. Because the digits have different length, my original substituting pattern ``[0-9],[0-9]'' didn't work. I had tried many different regexp patterns as I've known, but no one match my requirement. Then, I think I have to add some more knowledge about the regexp used in vim to improve my skill as well as the work efficiency, so I went to the help of Google and find the following website: Vim Regular Expressions 101.

Finally, I found what I needed. Some quantifiers can be used to set how many times certain part of the pattern should repeat. So my working pattern of regexp become ``[0-9]\+,[0-9]\+''. Learnt something new again. :-)

No comments:

Post a Comment