基本上,我正在遍历 html 文件并寻找几个正则表达式。它们匹配很好,但我不希望每个文件都包含匹配项,但是当循环运行时,每次迭代都包含相同的匹配项(尽管它不在该文件中)。我假设通过使用 $1 它在每次迭代中都保持不变。
我试过在每次真正的比赛后直接使用任意正则表达式来重置它,但这似乎不起作用。我从中得到这个想法的线程似乎有很多关于最佳实践和原始问题问题的争论等,所以我认为值得向我的代码寻求具体建议。它也可能写得不好:
# array of diff filenames
opendir(TDIR, "$folder/diff/$today") || die "can't opendir $today: $!";
@diffList = grep !/^\.\.?$/, readdir(TDIR);
closedir TDIR;
# List of diff files
print "List of Diff files:\n" . join("\n", @diffList) . "\n\n";
for($counter = 0; $counter < scalar(@diffList); $counter++) {
# Open diff file, read in to string
$filename = $diffList[$counter];
open FILE, "<", "$folder/diff/$today/$filename";
while(<FILE>) {
$lines .= $_;
}
close FILE or warn "$0: close today/$filename: $!";
# Use regular expressions to extract the found differences
if($lines =~ m/$plus1(.*?)$span/s) {
$plus = $1;
"a" =~ m/a/;
} else {$plus = "0";}
if($lines =~ m/$minus1(.*?)$span/s) {
$minus = $1;
"a" =~ m/.*/;
} else {$minus = "0";}
# If changes were found, send them to the database
if($plus ne "0" && $minus ne "0") {
# Do stuff
}
$plus = "0";
$minus = "0";
}
如果我在“do stuff”中放置一个打印件,它总是正确的,并且总是显示在其中一个文件中找到的相同的两个值。希望我已经很好地解释了我的情况。任何建议表示赞赏,谢谢。