Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern - Validating Filename 2

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
0
0
CA
Hi,

I trying to validate a filename so it has a least one character before the dot and three characters after the dot. (extension)

Any help would be appreciated.

 
I would use a RegExp search like this:

Code:
<cfif REFindNoCase(".+\.([a-zA-Z]{3,3})", FileName ) GT 0>

I'm sure that pattern can use a little tweaking, for instance it assumes that the extension doesn't have a number in it. It's just off the top of my head but it should get you going.


Travis Hawkins
 
Thanks tlhawkins

I ended with something like this
Code:
([a-zA-Z0-9_])\.([a-zA-Z]{3,4})

imstillatwork
I added {3,4} to support 4 charactes in extension.

If a string is let's say "filename.doc" or "filename.html", how can I simply remove everything before the dot & the dot keeping only the extension ? Using REReplace ? Any help would be apprecited.

Thanks
 
That looks pretty good, and you know what files may end up running through this, but keep in mind that there are a quite a few ther acceptable characters in a filename including a space, and there are no real rules about how many characters are allowed in the extension.

For instance both of our patterns would think that mysong.mp3 was not a filename... because we aren't checking for numbers in the extension. So, depending on what you're using it for, the pattern may need broadened a bit.

You have the concept down, though, so I'm sure you can make any adjustments that are needed.


Travis Hawkins
 
Any clue about:

If a string is let's say "filename.doc" or "filename.html", how can I simply remove everything before the dot + the dot keeping only the extension ? Using REReplace ?
 
Yeah,

I would use ReReplace. Something along these lines:

Code:
  ReReplace(filename, "^[a-zA-Z0-9_]+\.","")

The problem with this code, of course, is that a filename with an extra "." will throw it off. For instance "my.house.doc" will come back "house.doc". If that is an issue I would recommend using the other string functions and first reverse the string, then search for the first "." then grab up to that point with mid, or left, then reReverse what you have.

I hope that doesn't just confuse things ;)

Travis Hawkins
 
If a string is let's say "filename.doc" or "filename.html", how can I simply remove everything before the dot + the dot keeping only the extension ?
I think you may be over complicating things with the RegEx. I would treat it as a list:
Code:
ListLast('filename.doc','.')
That way, it doesn't matter how may "dots" you have, it will always search for the values after the last one.

As to your original question, it can easily be done by just checking the legnths of the list values.


Hope This Helps!

ECAR
ECAR Technologies

"My work is a game, a very serious game." - M.C. Escher
 

Thanks a lot tlhawkins
Since I'm forcing the filenames to be in a specific format (filename.xxx or filename.xxxx); it will be fine.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top