trevor1940
asked on
perl REGEX
Hi
I have a number of zip files that i need to rename based upon a line in a corresponding htm file
I'm then creating a dos bat file to do the renaming in the past I've had issues doing this in perl on windows.
I'd rather do the renaming in perl & test if the new file exists first
my code is erroring on the regex line 29
i'd apreicate if some one cauld look & tell me where i'm going wrong
thanx
I have a number of zip files that i need to rename based upon a line in a corresponding htm file
I'm then creating a dos bat file to do the renaming in the past I've had issues doing this in perl on windows.
I'd rather do the renaming in perl & test if the new file exists first
my code is erroring on the regex line 29
i'd apreicate if some one cauld look & tell me where i'm going wrong
thanx
#!/usr/bin/perl
$fulldir= "F:\\downloads\\cpg14x\\";
$zipalbum = $fulldir . "zipalbum\\";
print $fulldir;
opendir FULL, $zipalbum || die "Cannot open $fulldir";
@dir=grep /.zip$/i,readdir(FULL);
close (FULL);
print @dir;
my $bat= $fulldir . "\\ren.bat";
print "output file $bat";
open OUT, ">$bat" || die "fuck off";
$TH= "thumbnails.php?album=";
foreach my $Zip (@dir)
{
# sample file name between = & .zip is the $gal and this may be 1 2 or 3 digits
#zip.php^aid=12.zip
while ($Zip =~ m/aid=(\d)+.zip/)
{
my $gal =$1;
my $htm = $fulldir . $TH . $gal . ".htm";
open (INFILE, "$htm") || die "can't open $htm";
@infile = <INFILE>;
close(INFILE);
# sample line from htm
#<a href="thumbnails.php?album=12">Roses are Red</a></b></span></td>
foreach $line (@infile
{
while ($line =~ /<a href=\"thumbnails.php\?album=$gal\">(?.*)</\a><\/b><\/span><\/td>)/
{
my $newZip = $1; ## should = Roses are Red
$newZip = $zipalbum . $newZip . ".zip";
$old = $zipalbum . $Zip;
print OUT qq(move '$old' '$newZip');
}
}
}
}
In line 30, you have an unescaped $. This symbol stands for "end-of-line/end-of-string ". Using that symbol in this meaning would not make sense since you you have more regex following that symbol and no pattern modifiers which alter the meaning of $. Try escaping that symbol ( \$ ).
Actually, disregard. I missed that you declared that as a variable.
You do, however, need to escape any forward slashes ( / ) that occur within your pattern since you using that character as a pattern delimiter. You are missing an escape before the slash which occurs just before "\a". Alternatively, you can change the pattern delimiter to something which does not occur within your pattern. For example, you could use hash ( # ).
ASKER
hi
i've escaped the forward slashes so line 29 =
while ($line =~ m/<a href=\"thumbnails.php\?alb um=$gal\"> (?.*)<\/a> <\/b><\/sp an><\/td>) /
{
my $newZip = $1;
but get theese errors
syntax error at H:\PerlScripts\myPerl.pl line 29, near "while"
Can't use global $1 in "my" at H:\PerlScripts\myPerl.pl line 31, near "= $1"
syntax error at H:\PerlScripts\myPerl.pl line 38, near "}"
Execution of H:\PerlScripts\myPerl.pl aborted due to compilation errors.
i've escaped the forward slashes so line 29 =
while ($line =~ m/<a href=\"thumbnails.php\?alb
{
my $newZip = $1;
but get theese errors
syntax error at H:\PerlScripts\myPerl.pl line 29, near "while"
Can't use global $1 in "my" at H:\PerlScripts\myPerl.pl line 31, near "= $1"
syntax error at H:\PerlScripts\myPerl.pl line 38, near "}"
Execution of H:\PerlScripts\myPerl.pl aborted due to compilation errors.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanx for your help
i had to remove the ? from regex @ line 33 so (?.*) became (.*)
i had to remove the ? from regex @ line 33 so (?.*) became (.*)