For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > need help for regxp









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author need help for regxp
Chen Li

2006-10-20, 6:56 pm

Hi folks,

I have a folder containing a child folder and other
files. I want to print out the BMDC4-2.001 to
BMDC4-2.024 only and the file format is (string/number
or mix).number. Which regular expression is used to do
the job?

Thanks,

Li

########contents in the folder

folderx
Analysis-1.wsp
BMDC4-2.001
BMDC4-2.002
....
BMDC4-2.023
BMDC4-2.024

________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Rob Dixon

2006-10-20, 6:56 pm

chen li wrote:
> Hi folks,
>
> I have a folder containing a child folder and other
> files. I want to print out the BMDC4-2.001 to
> BMDC4-2.024 only and the file format is (string/number
> or mix).number. Which regular expression is used to do
> the job?
>
> Thanks,
>
> Li
>
> ########contents in the folder
>
> folderx
> Analysis-1.wsp
> BMDC4-2.001
> BMDC4-2.002
> ...
> BMDC4-2.023
> BMDC4-2.024


regexes can only match character patterns and can't make decisions based on the
numerical value of part of a string. Split the filenames into a name part and a
suffix part using a regex and compare them with the normal Perl operators.

HTH,

Rob


foreach (@folder) {

my ($name, $suffx) = /(.*)\.(.*)/;

next unless $name eq 'BMDC4-2';
next if $suffx =~ /\D/;
next unless $suffx >= 1 and $suffx <= 24;

print "$_\n";
}

John W. Krahn

2006-10-20, 6:56 pm

chen li wrote:
> Hi folks,


Hello,

> I have a folder containing a child folder and other
> files. I want to print out the BMDC4-2.001 to
> BMDC4-2.024 only and the file format is (string/number
> or mix).number. Which regular expression is used to do
> the job?



/\ABMDC4-2\.0(?:0[1-9]|1[0-9]|2[0-4])\z/



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Tom Phoenix

2006-10-20, 6:56 pm

On 10/20/06, Rob Dixon <rob.dixon@350.com> wrote:

> regexes can only match character patterns and can't make decisions
> based on the numerical value of part of a string.


That's misleading; matching character patterns *allows* one to make
decisions based on the numerical value of part of a string. Granted,
the way you showed is much more straightforward, efficient, and
practical for most purposes. But regexes can select any finite subset
of integers, for example, and many infinite ones as well. They are
surprisingly powerful.

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com