For Programmers: Free Programming Magazines  


Home > Archive > Tcl > August 2004 > regexp for glob









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 regexp for glob
Laurent Duperval

2004-08-26, 8:58 pm

Hi,

Is there a way to glob for file names that start with a lower-case
character, followed by three or more letters then ends with four or more
digits? Hence, mathing would yield:

foobar123456.txt = true
foo123456.txt = false
foobar_123456.txt = false
fooBar123456.txt = false
foobar123.txt = false

I know how to do it with regexp but how about glob? I guess I could get
all the files then match them with a regular expression but that seems
like overkill to me.

L
Bruce Hartweg

2004-08-26, 8:58 pm



Laurent Duperval wrote:
> Hi,
>
> Is there a way to glob for file names that start with a lower-case
> character, followed by three or more letters then ends with four or more
> digits? Hence, mathing would yield:
>
> foobar123456.txt = true
> foo123456.txt = false
> foobar_123456.txt = false
> fooBar123456.txt = false

this one seems to match you description. or do you mean ALL letters must be lower case
> foobar123.txt = false
>
> I know how to do it with regexp but how about glob? I guess I could get
> all the files then match them with a regular expression but that seems
> like overkill to me.
>

if you have "... or more" in you verbal description you can't do it
with glob, because the only multi char match is * which matches any number
of *any* chars. you will need an RE to do this fully. if there are
a large number of files, you could do a special glob that will prune
out some & then do a final check with the RE


if you meant all lower case letters (4+ lc chars, followed by 4+ digits)

set glob {[a-z][a-z][a-z][a-z]*[0-9][0-9][0-9][0-9]*}
set RE {^[a-z]{4,}[0-9]{4,}\.}

if only the first char needs lower cased

set glob {[a-z][a-zA-Z][a-zA-Z][a-zA-Z]*[0-9][0-9][0-9][0-9]*}
set RE {^[a-z][a-zA-Z]{3,}[0-9]{4,}\.}



foreach name [glob $glob] {
if [regexp $RE $name] {
lappend fileList $name
}
}


Bruce

Laurent Duperval

2004-08-27, 3:57 am

On Thu, 26 Aug 2004 18:19:49 -0500, Bruce Hartweg wrote:
> this one seems to match you description. or do you mean ALL letters must be lower case


All must be lower case. Sorry.

> if you have "... or more" in you verbal description you can't do it
> with glob, because the only multi char match is * which matches any number
> of *any* chars. you will need an RE to do this fully. if there are
> a large number of files, you could do a special glob that will prune
> out some & then do a final check with the RE
>


That's what I thought.

>
> if you meant all lower case letters (4+ lc chars, followed by 4+ digits)
>
> set glob {[a-z][a-z][a-z][a-z]*[0-9][0-9][0-9][0-9]*}
> set RE {^[a-z]{4,}[0-9]{4,}\.}
>


Thanks, that'll save me from actually figuring out the RE.

L


Donal K. Fellows

2004-08-27, 8:58 am

Laurent Duperval wrote:
> I know how to do it with regexp but how about glob? I guess I could get
> all the files then match them with a regular expression but that seems
> like overkill to me.


Glob-matching just isn't that powerful (which helps keep it fast) but
you could use it to pre-filter the filenames to remove obvious
failures before the final check with RE.

Donal.
Sponsored Links







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

Copyright 2008 codecomments.com