如果目录(及其子目录)中所有* .java文件的任何行中都有字符串(例如“ TODAY,TOMORROW,YESTERDAY”),则替换上面一行中的字符串(例如,将“下雨,多云,有风”更改为“晴天”),并在替换之前将它们打印到一个csv文件中(例如,file1.java今天下雨,file2.java TOMMOROW多云) 但是我的正则表达式无法正常工作。另外,还有其他更好的方法可以满足上述要求吗?
use strict;
use warnings;
use File::Find::Rule;
my @day = ("TODAY", "TOMORROW", "YESTERDAY");
my @weather = ("Raining", "Cloudy", "Windy");
my $dayregex = join "|", @day;
$dayregex = qr/\b($dayregex)\b/;
my $weatherregex = join "|", @weather;
$weatherregex = qr/\b($weatherregex)\b/;
my $output = 'output.csv';
#Getting list of files in dir and sub dirs
my @files = File::Find::Rule->file()
->name( '*.java' )
->in( 'C:/Users/path/to/folder/' );
for my $file (@files) {
print "Opening file: $file\n";
open(INPUT, $file) or die("Input file $file not found. \n");
while (my $line = <INPUT>) {
if ($line =~ m/$dayregex/ {
print "There was a match on $1 from array day\n";
#send the output to csv before replacing
open(OUTPUT, '>'.$output) or die("Cannot create $output file. \n");
print OUTPUT $file $1 $weatherregex;
close(OUTPUT);
#Replace the matched weatherregex string with Sunny
$line =~ s/$weatherregex(.*\n.*$1)/Sunny/g ;
}
}
}
close(INPUT);
答案 0 :(得分:-1)
尝试一下:
weatherregex(.*\n)+
我认为这将有助于查找weatherregex以多行显示或分为两行。