views:

66

answers:

2

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?

+5  A: 

Use pattern grouping like this:

:%s/something\[\(\d\+\)\]/somethingElse\1/g
B Johnson
`\d` is simplier, and I guess he may have several digits -> `\d\+`
Luc Hermitte
Good point, I'll update my answer
B Johnson
+1  A: 

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.

Jordan Lewis