Smart search/replace in Vim
I have a file with the following expressions:
something[0]
开发者_如何转开发
Where instead of 0 there could be different numbers. I want to replace all these occurances with
somethingElse0
Where the number should be the same as in the expression I replaced. How do I do that?
Use pattern grouping like this:
:%s/something\[\(\d\+\)\]/somethingElse\1/g
You should use a combination of match groups and character classes to accomplish this. Your example, if you mean replacing Something(numbers-here) with SomethingElse(same-numbers), is solved with the following command:
:%s/Something\(\d\+\)/SomethingElse\1/g
This finds all of the places in the file that have Something(a-bunch-of-digits), and replaces the Something with SomethingElse. The + means one or more digits; you could replace that with * if you wanted zero or more digits.
精彩评论