regex - Perl 脚本卡住了
问题描述
我有一个文件夹,其中包含需要删除而不删除正在运行的文件的旧文件。
我在 linux 中有一个命令来列出包含正在运行的文件名的正在运行的文件。
{"xxxxxx","235235235.filename",{xxx},xxxx,xxxx,xxxx}
我只需要235235235.filename
s 和一个脚本来比较 linux 目录中的文件并删除不匹配的文件,但是该目录也有xxxxxxxxxxx_235235235.filename
s。
这是我现在的位置:
#!/usr/bin/perl -w
use strict;
use warnings;
my $dir = "****";
opendir(DIR, $dir) or die "Could not open $dir: $!\n";
my @allfiles = readdir DIR;
close DIR;
foreach my $oldfiles(@allfiles) {
$oldfiles =~ s/^(?>\N*?_)((?<field3>[^\n\r.]*?)\.str)$/$+{field3}/img;
#print "$oldfiles\n";
}
my $cmd ="****";
#my $cmd ="ls";
my @runEvents = `$cmd`;
chomp @runEvents;
print "done.";
foreach my $running(@runEvents) {
$running =~ s/(?<field2>[0-9a-z]{4}_8[0-9a-z]{2}_[0-9a-z]{2}[a-z][0-9a-z]0[0-9]\.str|PICO_8D[0-9]_2{2}6[0-9]-1{2}\.str)|\{"PID 8[0-9a-z]{2}","/$+{field2}/ig;
print "$running\n";
}
当最后一个正则表达式运行时,我得到
在第 24 行的替换迭代器中使用未初始化的值 $+("field2"}。
谁能指出我一个好的方向?
解决方案
对于查找正则表达式失败的地方,我发现Regexp::Debugger非常有用。它允许您单步执行正则表达式,以便您可以查看正则表达式对于您的输入数据不够通用的地方。
来自“运行事件”程序的输入数据是诊断正则表达式失败原因的最有趣的地方,但如果有,将其放入一个小型测试程序中会使事情变得非常简单:
#!perl
use strict;
use warnings;
use Regexp::Debugger;
while(<DATA>) {
s/(?:(?<field2>[0-9A-Za-z]{4}_8[0-9A-Za-z]{2}_[0-9A-Za-z]{2}[A-Za-z][0-9A-Za-z]0[0-9]\.[Ss][Tt][Rr]|[Pp][Ii][Cc][Oo]_8[Dd][0-9]_2{2}6[0-9]-1{2}\.[Ss][Tt][Rr])|\{"[Pp][Ii][Dd] 8[0-9A-Za-z]{2}","|[",0-9ginru{}]{34,$
};
__DATA__
... the data that your shell script outputs ...
另外,您的正则表达式似乎被切断了,您能用正确的正则表达式更新您的问题吗?
推荐阅读
- python - 对 Unicode 字符串进行 XOR 加密
- ngrx - 在 NgRx 中,为什么动作类型从实际动作类中分离出来?
- python - 向量化结果/迭代模拟(在 python 中)
- javascript - Highcharts sunburst - 覆盖全局颜色
- sql - T-SQL - 将具有当前页面和以前页面的表转换为每个会话的顺序
- flutter - 我不能在堆栈中居中多个小部件
- gradle - 如何在 Gradle 中使用 pom 类型依赖
- amazon-web-services - Python 模块不在 AWS-EMR 的工作节点上
- octobercms - 当 OctoberCMS AJAX 调用 onSend 函数时,它不会执行它的代码
- qlikview - Qlik Sense:如何在 Google Drive 中指定路径?