我是Perl的新手,我正在閱讀文件中的文本,並希望用他們的法語翻譯替換一些單詞。我設法逐詞獲取,但不是通過表達式/字符串,我有問題讓它代碼明智。使用正則表達式在文件中搜索字符串Perl
代碼字一個字:
my $filename = 'assign3.txt';
my @lexicon_en = ("Winter","Date", "Due Date", "Problem", "Summer","Mark","Fall","Assignment","November");
my @lexicon_fr = ("Hiver", "Date", "Date de Remise","Problème","Été", "Point", "Automne", "Devoir", "Novembre");
my $i=1;
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file $filename !";
while (<$fh>) {
for my $word (split)
{
print " $i. $word \n";
$i++;
for (my $j=0; $j < 9;$j++){
if ($word eq $lexicon_en[$j]){
print "Found one! - j value is $j\n";
}
}
}
}
print "\ndone here!!\n";
這裏是正則表達式我試圖使用方法:
/\w+\s\w+/
這是我的字符串代碼:
while (<>) {
print ("this is text: $_ \n");
if ((split (/Due\sDate/),$_) eq "Due Date"){
print "yes!!\n";
}
}
你可以給你的正是尋找一些示例輸出。以便我可以向您發送示例腳本。 –