首页 > 解决方案 > 如何使用前缀从文件名中删除前导数字?

问题描述

我有很多这样的文件

0678 - Puzzle Series Vol. 5 - Slither Link (J).nds
0679 - Puzzle Series Vol. 6 - Illust Logic (J).nds
0680 - Puzzle Series Vol. 7 - Crossword 2 (J).nds

并想删除前四位数字和破折号,所以它们变成

Puzzle Series Vol. 5 - Slither Link (J).nds
Puzzle Series Vol. 6 - Illust Logic (J).nds
Puzzle Series Vol. 7 - Crossword 2 (J).nds

当我尝试时,prename我得到

$ prename -i 's/???? - (.+nds)/$1/' *nds
Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HERE ??? - (.+nds)/ at (eval 1) line 1.

问题

有人可以看到我做错了什么吗?

标签: regexperl

解决方案


?并不意味着你认为它意味着什么。这意味着前一个原子是可选的。例如/^ab?\z/匹配aab

用于.匹配除换行符以外的任何字符。/s使.任何字符,句号。

s/.... - (.+nds)/$1/

s/.{4} - (.+nds)/$1/          # Readability improvement.

s/.{4} - (.+nds)/$1/s         # No need to prevent `.` from matching a LF.

s/^.{4} - (.+nds)/$1/s        # Better to anchor for readability if nothing else.

s/^\d{4} - (.+nds)/$1/s       # A bit more restrictive, and more self-documenting.

s/^\d+ - (.+nds)/$1/s         # Do we really need to check that there are exactly 4?

s/^\d+ - (.*)/$1/s            # No point in checking if `nds` is contained.

s/^\d+ - //

推荐阅读