in reply to Re: regular expressions
in thread regular expressions

Hi it's me again, this is really what I'm trying to do, get each line of a file end if the word on the line begins with a has 1 or more words then ends with f or g (images files like amm.gif or ammre.jpg) get the word into a table @amourss.
#!usr/bin/perl # 2005-03-01 : # ouvrir le fichier contenant la liste des cartes open(CARTES, "imagescartes.txt") or die "Ouverture du ficher imagescar +tes.txt impossible: $!\n"; -T "imagescartes.txt" or print "ceci n'est pas un fichier texte\n"; while ($ligne = <CARTES>) { chop($ligne); while ($amourss .= /^a.+(f|g)$/g ) { $total++; } }

Replies are listed 'Best First'.
Re^3: regular expressions
by brian_d_foy (Abbot) on Mar 01, 2005 at 09:01 UTC

    If you are looking for particular filenames, I would expand the regular expression. I'd specify the file extension as much as possible, including the literal full stop that separates the name and extension. The /i flag is sometimes a good idea since some things like to make everything upper case.

    /^a.*\.(jpg|gif)$/i
    --
    brian d foy <bdfoy@cpan.org>
Re^3: regular expressions
by Random_Walk (Prior) on Mar 01, 2005 at 09:18 UTC

    I think this will do what you want.

    #!/usr/bin/perl # 2005-03-01: # Please always use strict and warnings, they will capture # many common mistakes and typos use strict; use warnings; # we may as well test it before we try to open it unless (-T 'imagescartes.txt') { print "ceci n'est pas un fichier texte\n"; exit 1; } # ouvrir le fichier contenant la liste des cartes open CARTES, '<', 'imagescartes.txt' or die 'Ouverture du ficher image +scartes.txt impossible: $!\n'; # this is the array where we store the image file names my @amourss; while (my $ligne = <CARTES>) { chomp $ligne ; if ($ligne =~ /^a\S+\.(?:gif|jpg)$/) # will match exactly one word # begining a and ending in .g +if or .jpg # the \S+ is one or more non +space chrs # a.gif not allowed use \S* t +o allow it { push @amourss, $ligne; } } close CARTES; print "found ", scalar @amourss, " images: "; print join ", ", @amourss; print "\n"; __END__ # input file used test.gif this asilly.gif abc.jpg abcd.gif absolutely not a.jpg # results of running >./amourss found 2 images: abc.jpg, abcd.gif >

    Update

    as Jasper points out a space is valid in filenames in most OSes so please feel free to change the regex to /^a.+\.(?:gif|jpg)$/ if you wish to allow spaces in filenames or /^a.*\.(?:gif|jpg)$/ if you wish to allow a.gif and a.jpg

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
      I don't think this is particularly good, because "this asilly.gif" and "absolutely not a.jpg", even though you've given them stupid names, are valid filenames.

        Doh ! thanks for pointing that out. I had in my mind that the OP wanted to filter out lines of text from lines containing only the filename. If the filename can contain spaces then this becomes impossible. Of course this is probably not what the OP wants. I guess he more likely wants to get the files from a directory listing or such like. I Never have been a fan of filenames with spaces in them

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!