论坛首页 海阔天空论坛

关于perl的一个问题

浏览 2447 次
精华帖 (0) :: 良好帖 (0) :: 灌水帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-25   最后修改:2009-07-25
今天没事用perl想着弄一个批量修改文件名的小代码
自己对perl不是很熟悉
在网上随便找了一个
for $file [color=red](glob "*.mp3")[/color]{
	@fileArray =split(/-\s/,$file); #01.If I Were a Boy.mp3 --》If I Were a Boy.mp3
	rename($file ,$fileArray[1]);
}


这里的 glob "*.mp3" 看的意思很大概
谁能给科普下.
   发表时间:2009-07-25  
是不是和unix中的grep一样?
0 请登录后投票
   发表时间:2009-07-25   最后修改:2009-07-25
glob 返回匹配的文件名的数组。

glob 的规则:
* 匹配任意长字符串(0长也可以), ? 匹配任意字符, ** 匹配任意层级的子目录

glob "*.mp3" 返回把当前目录下的所有 mp3 文件名的数组
glob "**/*.mp3" 包含所有子目录及孙目录及……里面的 mp3 文件名

假设目录结构是
a.mp3
dd/
  b1.mp3
  b2.mp3
  cc/
    c.mp3

glob "**/*.mp3" 返回 ("a.mp3","dd/b1.mp3","dd/b2.mp3","dd/cc/c.mp3")
glob "*.mp3" 返回 ("a.mp3")
glob "dd/?1.mp3" 返回 ("dd/b1.mp3")

Ruby 里面的 Dir.glob 就是从 perl 里搬过去的。

引用
*: Matches any file. Can be restricted by other values in the glob. * will match all files; c* will match all files beginning with c; *c will match all files ending with c; and c will match all files that have c in them (including at the beginning or end). Equivalent to / .* /x in regexp.
**: Matches directories recursively.
?: Matches any one character. Equivalent to /.{1}/ in regexp.
[set]: Matches any one character in set. Behaves exactly like character sets in Regexp, including set negation ([^a-z]).
{p,q}: Matches either literal p or literal q. Matching literals may be more than one character in length. More than two literals may be specified. Equivalent to pattern alternation in regexp.
0 请登录后投票
论坛首页 海阔天空版

跳转论坛:
Global site tag (gtag.js) - Google Analytics