1
讓我有一個文本文件包含Text.txt
如何更換正則表達式作爲普通的文本從* INI文件
\mathbf{\mathit{adad09}
\mathit{\mathit{aavB}
\mathul{\mathit{AZDs}
而且我有一個test.ini
文件包含(因爲用戶可以添加或更少的替代)
<repl>
\mathbf{\mathit{([A-z0-9]*)} \boldsymbol{\mathit{$1}
\mathit{\mathit{([A-z0-9]*)} \italicsymbol{\mathit{$1}
\mathul{\mathit{([A-z0-9]*)} \underlinesymbol{\mathit{$1}
</repl>
我的預期輸出是
\boldsymbol{\mathit{adad09}
\italicsymbol{\mathit{aavB}
\underlinesymbol{\mathit{AZDs}
我的編碼是這裏
use strict;
use warnings;
use Cwd;
my $myinicnt = ""; my $file = "Test.txt"; my $str = "";
readFileinString($file, \$str); readFileinString("Test.ini", \$myinicnt);
my @replsEach =();
if($myinicnt=~m/<repl>(.*?)<\/repl>/gs) { @replsEach = split /\n/, $1; }
foreach my $myvar(@replsEach)
{
my ($val1, $val2) = split /\t/, $myvar;
my $quval1 = quotemeta $val1;
#First Attempt
$str=~s{$val1}{$val2}gi;
#Second Attempt
$str=~s{$quval1}{$val2}gi;
}
print $str;
#---------------> File reading
sub readFileinString
#--------------->
{
my $File = shift;
my $string = shift;
open(FILE1, "<$File") or die "\nFailed Reading File: [$File]\n\tReason: $!";
read(FILE1, $$string, -s $File, 0);
close(FILE1);
}
#---------------> File Writing
sub writeFileinString
#--------------->
{
my $File = shift;
my $string = shift;
my @cDir = split(/\\/, $File);
my $tmp = "";
for(my $i = 0; $i < $#cDir; $i++)
{
$tmp = $tmp . "$cDir[$i]\\";
mkdir "$tmp";
}
if(-f $File){
unlink($File);
}
open(FILE, ">$File") or die "\n\nFailed File Open for Writing: [$File]\n\nReason: $!\n";
print FILE $$string;
close(FILE);
}
#------------------------- SUB END ------------------------->
錯誤:
Use of uninitialized value $val1 in quotemeta at ini.pl line 13.
Use of uninitialized value $val2 in concatenation (.) or string at ini.pl line 18.
Use of uninitialized value $val1 in regexp compilation at ini.pl line 19.
Unrecognized escape \m passed through in regex; marked by <-- HERE in m/\m <-- HERE athbf{\mathit{([A-z]*)}/ a
t ini.pl line 19.
Unrecognized escape \m passed through in regex; marked by <-- HERE in m/\mathbf{\m <-- HERE athit{([A-z]*)}/ a
t ini.pl line 19.
Unrecognized escape \m passed through in regex; marked by <-- HERE in m/\m <-- HERE athit{\mathit{([A-z]*)}/ a
t ini.pl line 19.
Unrecognized escape \m passed through in regex; marked by <-- HERE in m/\mathit{\m <-- HERE athit{([A-z]*)}/ a
t ini.pl line 19.
Unrecognized escape \m passed through in regex; marked by <-- HERE in m/\m <-- HERE athul{\mathit{([A-z]*)}/ a
t ini.pl line 19.
Unrecognized escape \m passed through in regex; marked by <-- HERE in m/\mathul{\m <-- HERE athit{([A-z]*)}/ a
t ini.pl line 19.
\mathbf{\mathit{adad09}
\mathit{\mathit{aavB}
\mathul{\mathit{AZDs}
請會有人可以指導我得到的解決方案。
冠軍我無法得到所需的輸出....幫助我... – ssr1012
錯誤更新在我的問題請 – ssr1012
你還沒有反斜槓的所有反斜槓。 – choroba