in reply to Re^2: regular expressions
in thread regular expressions
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 >
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: regular expressions
by Jasper (Chaplain) on Mar 01, 2005 at 10:13 UTC | |
by Random_Walk (Prior) on Mar 01, 2005 at 13:47 UTC |