$filename =~ s,^_\d+-(.*?)--(.*?)_(.*?)_$,\u\1 - \u\2 (\u\3),;
我的Perl解釋器(使用嚴格和警告)說,這是更好的寫法如下:
$filename =~ s,^_\d+-(.*?)--(.*?)_(.*?)_$,\u$1 - \u$2 (\u$3),;
第大概一個更多sedish它的味道! (當然,這兩個版本的作品一樣的。)
說明(如要求由stema):
$filename =~ s/
^ # matches the start of the line
_\d+- # matches an underscore, one or more digits and a hypen minus
(.*?)-- # matches (non-greedyly) anything before two consecutive hypen-minus
# and captures the entire match (as the first capture group)
(.*?)_ # matches (non-greedyly) anything before a single underscore and
# captures the entire match (as the second capture group)
(.*?)_ # does the same as the one before (but captures the match as the
# third capture group obviously)
$ # matches the end of the line
/\u$1 - \u$2 (\u$3)/x;
在替換指定的\u${1..3}
只是告訴Perl從1將拍攝組3與他們的第一個人物造大寫。如果你想讓整個比賽(在一個被捕獲的組中)大寫,你必須改用\U
。
的X標誌開啓詳細模式,它告訴我們要使用#評論Perl解釋器,所以它會忽略這些(和正則表達式的任何空白 - 所以,如果你想匹配空間你必須使用\s
或\
)。不幸的是,我無法弄清楚如何讓Perl忽略*替換*規範中的空白 - 這就是爲什麼我在一行中寫入的原因。
(另請注意,我已經改變了我s
終止從,
到/
- Perl的咆哮在我,如果我用,
用詳細模式開啓...不知道是什麼原因)
謝謝了! s/-/- /; < - 不會用' - '替換每個' - '?另外,如何在文件名的末尾添加')'? – Naveen
我剛剛意識到我的錯誤。我有一些文件命名如上例所示,一些文件命名爲00-author-book-revision。無論哪種情況,我都希望將文件重命名爲Author - Book(修訂版)。但是你的輸入肯定幫助了我,我將找出如何解決這個問題。 – Naveen
@Naveen,沒有/ g,它只會取代第一個。 ''s。=')';' – ikegami