Friday, May 6, 2011

Regular expression to replace _

I have this pattern

a,abc_def_eghi
1,234_556
5,567_987_ghi

I want to replace the first _ with a ",". I know %s/old/new/g to replace contents in vim.

Result

a,abc,def_eghi
1,234,556
5,567,987_ghi

Could you suggest some alternatives to go about it

From stackoverflow
  • The "g" is for "global". If you leave it off, the substitution will apply only once on each line.

    %s/old/new/

  • If you only want to replace the first occurrence of a match, do not use the g modifier. That is, use s/old/new/ instead of s/old/new/g. More vim search/replace tips and tricks can be found over at Wikia.

0 comments:

Post a Comment