I have some issues with a regular expression in perl. I'm trying to add a string at the beginning of a another string (in fact, insert a string at the beginning of the name of a file). What I want is check before inserting that string if the file already begins by it.
This is the code I have:
if ($ficheroSinExt !~ m/^$strCadena/){
# if it doesn't exist at the beginning, I insert it...
$ficheroSinExt = $strCadena . " " . $ficheroSinExt;
}
else{
print "---->It already exists!!!\n";
}
I'm testing it with two filenames with only one containing [Perl] at the beginning ("[Perl] File1.pdf" and "File2.pdf"), and $strCadena contains [Perl]. I end up adding [Perl] for both files, so their new names are "[Perl] [Perl] File1.pdf" and "[Perl] File2.pdf".
I think the problem comes from the ^$strCadena of the match operator, but I don't arrive to work-around it. Could you please give me a hand?
Thanks in advance, Diego
$ficheroSinExt =~ s/^(?! \Q$strCadena\E )/$strCadena /x;