我試圖在 HTML 檔案中找到標記為“\v{G}”或“\v{g}”的句子中的一些特殊字符,分別用“?”和“?”替換它們,并保存更正的句子在一個新的 HTML 檔案中。
我的正則運算式(.*)\\v\{(\w)\}(.*)找到要替換的字符,但我無法根據其大小寫替換該字符:生成的檔案包含:
This is a sentence ? with a upper case G.
This is a sentence ? with a lower case g.
代替:
This is a sentence ? with a upper case G.
This is a sentence ? with a lower case g.
MWE
HTML 輸入檔案包含:
This is a sentence \v{G} with a upper case G.
This is a sentence \v{g} with a lower case g.
perl 檔案包含:
use strict;
use warnings;
# Define variables
my ($inputfile, $outputfile, $inputone, $inputtwo, $part1, $specialcharacter, $part2);
# Initialize variables
$inputfile = "TestFile.html";
$outputfile = 'Results.html';
# Open output file
open(my $ofh, '>:encoding(UTF-8)', "$outputfile");
# Open input file
open(my $ifh, '<:encoding(UTF-8)', "$inputfile");
# Read input file
while(<$ifh>) {
# Analyse _temp.html file to identify special characters
($part1, $specialcharacter, $part2) = ($_ =~ /(.*)\\v\{(\w)\}(.*)/);
if ($specialcharacter == "g") {
$specialcharacter = "ǧ";
}elsif ($specialcharacter == "G") {
$specialcharacter = "Ǧ";# PROBLEM
}
say $ofh "\t\t<p>$part1$specialcharacter$part2";
}
# Close input and output files
close $ifh;
close $ofh;
uj5u.com熱心網友回復:
正如評論中提到的,==是錯誤的運營商。您應該使用來比較非數字標量eq。
另一種方法是創建一種字典形式、一個查找表,并在其中查找您的特殊字符。
# A map between the special characters and the html code you want in its place.
# Fill it with more if you've got them.
my %SpecialMap = (
'g' => 'ǧ',
'G' => 'Ǧ',
);
# Read input file
while(<$ifh>) {
# loop for as long as \v{character} is found in $_
while(/\\v\{(\w)\}/) {
# Look up the character in the dictionary.
# Fallback if it's not in the map: Use the character as-is instead.
my $ch = $SpecialMap{$1} || $1;
# Rebuild $_
$_ = $` . $ch . $';
}
# print the result
print $ofh $_;
}
對于輸入
Both \v{g} and \v{G} in here.
This is a sentence \v{g} with a lower case g.
This is a sentence \v{H} with a upper case H which is not in the map.
This contains nothing special.
它將產生以下輸出:
Both ǧ and Ǧ in here.
This is a sentence ǧ with a lower case g.
This is a sentence H with a upper case H which is not in the map.
This contains nothing special.
受 Polar Bear 評論的啟發,您可以改用s///ge執行映射函式并獲得相同的結果:
my %SpecialMap = (
'g' => 'ǧ',
'G' => 'Ǧ',
);
sub mapfunc {
return $SpecialMap{$1} || $1;
}
# Read input file
while(<$ifh>) {
# /g substitute all matches on the line
# /e by executing mapfunc($1) for each
s/\\v\{(\w)\}/mapfunc($1)/ge;
print $ofh $_;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/485309.html
