Solved

perl REGEX

Posted on 2011-09-02
6
280 Views
Last Modified: 2012-08-14
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
#!/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');
                
              }
           }
      }
  }

Open in new window

0
Comment
Question by:trevor1940
  • 3
  • 2
6 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36473225
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 ( \$ ).
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36473232
Actually, disregard. I missed that you declared that as a variable.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36473251
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 ( # ).
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:trevor1940
ID: 36473519
hi

i've escaped the forward slashes so line 29 =

            while ($line =~ m/<a href=\"thumbnails.php\?album=$gal\">(?.*)<\/a><\/b><\/span><\/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.
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 36473815
I see three errors in your script.  This should fix it.

You should *always* use strict and warnings.  It will help you find errors much more easily.

Changes (line #s are from your code attachment):
* add missing ) to lines 28 and 30
* correct escape of slash on line 30 (you have /\ but should be \/)
* add my to var declarations so use strict doesn't complain

#!/usr/bin/perl

use strict;
use warnings;

my $fulldir= "F:\\downloads\\cpg14x\\";
my $zipalbum = $fulldir . "zipalbum\\";

print $fulldir;
opendir FULL, $zipalbum || die "Cannot open $fulldir";
my @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";
my $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";
        my @infile = <INFILE>;
        close(INFILE);
        # sample line from htm
        #<a href="thumbnails.php?album=12">Roses are Red</a></b></span></td>
        foreach my $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";
                my $old = $zipalbum . $Zip;
                print OUT qq(move '$old' '$newZip');
            }
        }
    }
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:trevor1940
ID: 36474877
thanx for your help
i had to remove the ? from regex @ line 33  so (?.*) became (.*)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now