tags:

views:

175

answers:

3

can someone please edit %:s/\([0-9]*\)_\(*\)/\2 so that i can rename files. for example, if file name is 5555_word_word.jpg, then I want the file name to be word_word.jpg. i feel like I am so close!

+3  A: 

Try this:

:%s/\([0-9]*\)_\(.*\)/\2

The . will match any character (part of the second grouping) and the * will greedily match any amount of them. Your original regex was missing that directive. This will also rename files of the form _word_word.txt to word_word.txt. If you want to require digits to match (probably a good idea), use:

:%s/\([0-9]\+\)_\(.*\)/\2

The \+ directive means to match 1 or more instances.

Jason
A problem with this is that if you have "word_word.jpg" it will become "word.jpg" because the leading `[0-9]` are optional. You have to be careful with greedy operators like `*`.
Seth Johnson
Neither of these work because `()` need to be backslashed. So does `+`.
Brian Carper
@Brian: judging by the OP's use of the parens, I'd guess you're correct, however it is possible to change the "magic" setting of the regex such that this one would work (I think)
rmeador
also, you have : and % in the wrong order.
rampion
OK, given that this is the accepted answer I have fixed it so that it will work.
Brian Carper
+6  A: 

You may want to simplify and have it just delete leading numbers and the underscore:

s/^[0-9]+_//
Seth Johnson
+3  A: 

Your version is fine but you forgot a period and you should probably anchor it to the beginning of a line or to a word boundary using either ^ or \<.

:%s/^\([0-9]*\)_\(.*\)/\2/

You can use \v to clean up some of those slashes.

:%s/\v^([0-9]*)_(.*)/\2/

You can use \ze to avoid capture groups.

:%s/^[0-9]*_\ze.*//

But the trailing .* is superfluous, because it matches anything. So use Seth's version, it's the simplest.

Brian Carper
+1 for \ze I didn't know that and I can see where it might come in handy
Blixtor