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!
有人可以编辑%:s / \([0-9] * \)_ \(* \)/ \ 2,以便我可以重命名文件。例如,如果文件名是5555_word_word.jpg,那么我希望文件名是word_word.jpg。我觉得我很亲密!
3 个解决方案
#1
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:
的。将匹配任何字符(第二个分组的一部分),*将贪婪地匹配任何数量的字符。你的原始正则表达式缺少该指令。这也会将_word_word.txt形式的文件重命名为word_word.txt。如果你想要求数字匹配(可能是个好主意),请使用:
:%s/\([0-9]\+\)_\(.*\)/\2
The \+
directive means to match 1 or more instances.
\ +指令表示匹配1个或多个实例。
#2
You may want to simplify and have it just delete leading numbers and the underscore:
您可能希望简化并让它只删除前导数字和下划线:
s/^[0-9]+_//
#3
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 \<
.
您的版本没问题,但是您忘记了一段时间,您可能应该使用^或\ <将其锚定到行的开头或单词边界。< p>
:%s/^\([0-9]*\)_\(.*\)/\2/
You can use \v
to clean up some of those slashes.
您可以使用\ v来清除其中一些斜杠。
:%s/\v^([0-9]*)_(.*)/\2/
You can use \ze
to avoid capture groups.
您可以使用\ ze来避免捕获组。
:%s/^[0-9]*_\ze.*//
But the trailing .*
is superfluous, because it matches anything. So use Seth's version, it's the simplest.
但尾随。*是多余的,因为它匹配任何东西。所以使用Seth的版本,它是最简单的。
#1
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:
的。将匹配任何字符(第二个分组的一部分),*将贪婪地匹配任何数量的字符。你的原始正则表达式缺少该指令。这也会将_word_word.txt形式的文件重命名为word_word.txt。如果你想要求数字匹配(可能是个好主意),请使用:
:%s/\([0-9]\+\)_\(.*\)/\2
The \+
directive means to match 1 or more instances.
\ +指令表示匹配1个或多个实例。
#2
You may want to simplify and have it just delete leading numbers and the underscore:
您可能希望简化并让它只删除前导数字和下划线:
s/^[0-9]+_//
#3
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 \<
.
您的版本没问题,但是您忘记了一段时间,您可能应该使用^或\ <将其锚定到行的开头或单词边界。< p>
:%s/^\([0-9]*\)_\(.*\)/\2/
You can use \v
to clean up some of those slashes.
您可以使用\ v来清除其中一些斜杠。
:%s/\v^([0-9]*)_(.*)/\2/
You can use \ze
to avoid capture groups.
您可以使用\ ze来避免捕获组。
:%s/^[0-9]*_\ze.*//
But the trailing .*
is superfluous, because it matches anything. So use Seth's version, it's the simplest.
但尾随。*是多余的,因为它匹配任何东西。所以使用Seth的版本,它是最简单的。