简体   繁体   English

Perl Regex字符串替换无法识别字符串中的下划线

[英]Perl Regex string replacement is not recognizing underscores in a string

I'm trying to batch rename a bunch of files using Perl and Regex. 我正在尝试使用Perl和Regex批量重命名一堆文件。 I've been able to change most things so far but I'm having trouble removing underscores. 到目前为止,我已经能够更改大多数事情,但是在删除下划线时遇到了麻烦。 I've search and found several examples, all with similar syntax but for some reason it's not working for me. 我搜索并找到了几个示例,它们的语法都差不多,但由于某些原因,它对我不起作用。

Here's an an example of what my file names look like: 这是我的文件名的示例:

HP_1 Level 1 Geology_Plan_1_400dpi.jpg HP_1 1级Geology_Plan_1_400dpi.jpg

Here's my code so far: 到目前为止,这是我的代码:

#  Declare directory path
my $dir = './Georeferenced_Images.tri/TEST/';

#  Initialization message
printf "Changing names...\n";

#  Remove spaces
my @list = glob("$dir/*");
for (@list) {
      my $orig = $_;
      s/\s+//g;
      move($orig, $_);  
}

#  Remove underscores
@list = glob("$dir/*");
for (@list) {
      my $orig = $_;
      s/_//g;
      move($orig, $_);  
}

The top part of the code removes the spaces. 代码的顶部删除了空格。 The second part of the code is not removing the underscores. 代码的第二部分不删除下划线。 I've played around with the code and it works if I do something like: 我玩过这些代码,如果执行以下操作,它将起作用:

s/_Plan_//g;

It doesn't work if I just try to remove the underscores. 如果我只是尝试删除下划线,那将不起作用。 Any help would be great! 任何帮助将是巨大的!

Thanks 谢谢

The problem is that your directory name contains an underscore. 问题是您的目录名称包含下划线。 So when you remove all of the underscores from $_ , you change the name of the directory and the move() tries to move your renamed file into a renamed directory which (presumably) doesn't exist. 因此,当您从$_删除所有下划线时,您将更改目录的名称,并且move()尝试将重命名的文件移动到(大概)不存在的重命名目录中。

One solution is to use File::Basename to split $_ into the directory name and the filename and to only change the filename. 一种解决方案是使用File :: Basename$_拆分为目录名和文件名,并且仅更改文件名。

Another solution would be to change directory into $dir before calling glob() (and then removing $dir/ from the parameter you pass to glob() ). 另一个解决方案是在调用glob()之前将目录更改为$dir (然后从传递给glob()的参数中删除$dir/ )。

You might have seen what the problem was is you have checked the return value of move() and displayed an appropriate error message. 您可能已经看到问题所在,是您检查了move()的返回值并显示了相应的错误消息。

move($orig, $_)
  or die "Could not move $orig to $_: $!";

Also note, that by printing out $orig and $_ before calling move() you would have a) probably seen what the problem was and b) realised that the problem has nothing to do with Perl's string replacement (as you would have seen that the replacement was carried out successfully). 还要注意,通过在调用move()之前打印出$orig$_ ,您可能会a)可能看到了问题所在,并且b)意识到该问题与Perl的字符串替换无关(如您所见,替换已成功执行)。 This is, surely, basic debugging practice and would have avoided (at the very least) you posting a question with a completely misleading title :-) 当然,这是基本的调试实践,并且可以避免(至少)避免您发布的标题完全误导的问题:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM