文件名中的转义括号
我有一些名为以下内容的文件:file (2).jpg
。我正在编写一个 Perl 脚本来重命名它们,但由于括号未被替换而出现错误。 所以。有人可以告诉我如何转义字符串中的所有括号(和空格,如果它们引起问题),以便我可以将其传递给命令。脚本如下:
#Load all jpgs into an array. @pix = `ls *.JPG`; foreach $pix (@pix) { #Let you know it's working print "Processing photo ".$pix; $pix2 = $pix; $pix2 =~ \Q$pix\E; # Problem line #Use the program exiv2 to rename the file with timestamp system("exiv2 -r %Y_%m%d_%H%M%S $pix2"); }
错误是这样的:
Can't call method "Q" without a package or object reference at script.sh line [problem line].
这是我第一次使用正则表达式,所以我正在寻找解释要做什么以及给出答案的答案。感谢您的任何帮助。
I've got a few files named stuff like this: file (2).jpg
. I'm writing a little Perl script to rename them, but I get errors due to the brackets not being replaced. So. Can someone tell me how to escape all the brackets (and spaces, if they cause a problem) in a string so I can pass it to a command. The script is below:
#Load all jpgs into an array. @pix = `ls *.JPG`; foreach $pix (@pix) { #Let you know it's working print "Processing photo ".$pix; $pix2 = $pix; $pix2 =~ \Q$pix\E; # Problem line #Use the program exiv2 to rename the file with timestamp system("exiv2 -r %Y_%m%d_%H%M%S $pix2"); }
The error is this:
Can't call method "Q" without a package or object reference at script.sh line [problem line].
This is my first time with regex, so I'm looking for the answers that explain what to do as well as giving an answer. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用简单的呢?
附:
\Q 禁用模式元字符,直到 \E 在正则表达式内。
例如,如果你想匹配路径“../../../somefile.jpg”,你可以写:
而不是
Why do not use a simple?
Ps:
The \Q disabling pattern metacharacters until \E inside the regex.
For example, if you want match a path "../../../somefile.jpg", you can write:
instead of
我发现这个 Perl 重命名脚本是 Larry Wall 不久前编写的......它可以满足您的需要,甚至更多。我保留在我的 $PATH 中,并每天使用它......
I found this perl renaming script that was written by Larry Wall a while back... it does what you need and so much more. I keep in in my $PATH, and use it daily...