Reputation: 2589
I have a text file contains Text.txt
\mathbf{\mathit{adad09}
\mathit{\mathit{aavB}
\mathul{\mathit{AZDs}
And I have a test.ini
file contains (Since user can add or less the replacements)
<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>
My Expected output is
\boldsymbol{\mathit{adad09}
\italicsymbol{\mathit{aavB}
\underlinesymbol{\mathit{AZDs}
My Coding is here
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 ------------------------->
Error:
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}
Please could anyone can be guide me to get the solution.
Upvotes: 1
Views: 107
Reputation: 241968
If you print the $quval1, you'll see what the problem is:
\\mathbf\{\\mathit\{\(\[A\-z0\-9\]\*\)\}
~~~~ ~~ ~~ ~~~~~~
quotemeta backslashes the dashes, parentheses, square brackets, and asterisks, as well, but you need to keep them unbackslashed.
But, how do you recognize which of these characters are part of a regex and which are part of the TeX markup?
Store the regexes in the file already quoted.
That doesn't end the story, though. The $1
is the replacement isn't expanded, because it's not contained in the replacement literally. You can use the /e
switch to evaluate the replacement, but then you need to backslash backslashes in it again, and in fact you need the double /ee
to evaluate the code properly, so you need to wrap the string in quotes to make it syntactically correct.
Data:
<repl>
\\mathbf\{\\mathit\{([a-zA-Z0-9]*)\} \\boldsymbol{\\mathit{$1}
\\mathit\{\\mathit\{([a-zA-Z0-9]*)\} \\italicsymbol{\\mathit{$1}
\\mathul\{\\mathit\{([a-zA-Z0-9]*)\} \\underlinesymbol{\\mathit{$1}
</repl>
Code:
$val2 = qq("$val2");
$str =~ s/$val1/$val2/geei;
I also had to fix the file names, as Text
, Test
, and test
are three different strings.
Upvotes: 2