473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Regular Expression to validate file extension

Does anyone know a regular expression that will validate the file extension
but also allow multiple file extensions if necessary. It also needs to be
case insensitive. Basically, what I want is to validate a file input box to
check if the extension is the correct type, i.e. .doc for a Word Document
etc. Also I would like to check multiple file types, for instance allow a
gif or a jpeg or a jpg.

Regards, Chris.
Nov 18 '05 #1
7 25346
I responded to this exact same question yesterday.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chris Kennedy" <ch**********@cybase.co.uk> wrote in message
news:#g**************@TK2MSFTNGP09.phx.gbl...
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be
case insensitive. Basically, what I want is to validate a file input box to check if the extension is the correct type, i.e. .doc for a Word Document
etc. Also I would like to check multiple file types, for instance allow a
gif or a jpeg or a jpg.

Regards, Chris.

Nov 18 '05 #2
Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Format(" {0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Format(" {0} NOT OK", test));
}
}

Hope this helps

--
____________________
www.bloomfield.as.ro

Chris Kennedy wrote:
Does anyone know a regular expression that will validate the file extension
but also allow multiple file extensions if necessary. It also needs to be
case insensitive. Basically, what I want is to validate a file input box to
check if the extension is the correct type, i.e. .doc for a Word Document
etc. Also I would like to check multiple file types, for instance allow a
gif or a jpeg or a jpg.

Regards, Chris.

Nov 18 '05 #3
Sorry about repost but my news server sometimes gets confused and appends
the question to the wrong thread. I couldn't reply because I couldn't find
it. You can sometimes find it on Google but not always. Has question not
clarified things. I want an expression to check for a variable number of
expressions in the extension of a file path e.g. jpeg or jpg or gif. I hope
this clarifies things. Regards, Chris.
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:uq**************@TK2MSFTNGP11.phx.gbl...
I responded to this exact same question yesterday.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chris Kennedy" <ch**********@cybase.co.uk> wrote in message
news:#g**************@TK2MSFTNGP09.phx.gbl...
Does anyone know a regular expression that will validate the file

extension
but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to validate a file input box

to
check if the extension is the correct type, i.e. .doc for a Word Document etc. Also I would like to check multiple file types, for instance allow a gif or a jpeg or a jpg.

Regards, Chris.


Nov 18 '05 #4
In that case, Chris, it looks like boomfield has already guessed your
question, and given you a good answer!

BTW, there is an excellent freeware utility for building and testing Regular
Expressions, which you can download from:

http://www.weitz.de/regex-coach/

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chris Kennedy" <ch**********@cybase.co.uk> wrote in message
news:u$**************@TK2MSFTNGP11.phx.gbl...
Sorry about repost but my news server sometimes gets confused and appends
the question to the wrong thread. I couldn't reply because I couldn't find
it. You can sometimes find it on Google but not always. Has question not
clarified things. I want an expression to check for a variable number of
expressions in the extension of a file path e.g. jpeg or jpg or gif. I hope this clarifies things. Regards, Chris.
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:uq**************@TK2MSFTNGP11.phx.gbl...
I responded to this exact same question yesterday.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chris Kennedy" <ch**********@cybase.co.uk> wrote in message
news:#g**************@TK2MSFTNGP09.phx.gbl...
Does anyone know a regular expression that will validate the file extension
but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to validate a file input
box
to
check if the extension is the correct type, i.e. .doc for a Word

Document etc. Also I would like to check multiple file types, for instance
allow a gif or a jpeg or a jpg.

Regards, Chris.



Nov 18 '05 #5
It tried putting this in the validation expression of my regular expression
validator, got it to validate a textbox with qwerty.jpg as the text value
and it didn't work. Any ideas? Regards, Chris.

"bloomfield" <bl********@as.ro> wrote in message
news:#K*************@TK2MSFTNGP11.phx.gbl...
Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Format(" {0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Format(" {0} NOT OK", test));
}
}

Hope this helps

--
____________________
www.bloomfield.as.ro

Chris Kennedy wrote:
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to validate a file input box to check if the extension is the correct type, i.e. .doc for a Word Document etc. Also I would like to check multiple file types, for instance allow a gif or a jpeg or a jpg.

Regards, Chris.

Nov 18 '05 #6
Sorry I'll be more specific, by didn't work I mean it it saw valid (ie
qwerty.jpg) and invalid strings (eg qwerty.doc) as invalid.
"Chris Kennedy" <no****@nospam.co.uk> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
It tried putting this in the validation expression of my regular expression validator, got it to validate a textbox with qwerty.jpg as the text value
and it didn't work. Any ideas? Regards, Chris.

"bloomfield" <bl********@as.ro> wrote in message
news:#K*************@TK2MSFTNGP11.phx.gbl...
Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Format(" {0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Format(" {0} NOT OK", test));
}
}

Hope this helps

--
____________________
www.bloomfield.as.ro

Chris Kennedy wrote:
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to validate a file input
box
to check if the extension is the correct type, i.e. .doc for a Word Document etc. Also I would like to check multiple file types, for instance
allow
a gif or a jpeg or a jpg.

Regards, Chris.


Nov 18 '05 #7
I tried the expression ^.+\.((jpg)|(gif)|(jpeg))$ and it worked for me
in a RegularExpressionValidator. The only problem was with the string
case. I didn't find a way to specify that the regular expression is case
insensitive for the RegularExpressionValidator. The expression can be
rewritten to
^.+\.(([jJ][pP][eE]?[gG])|([gG][iI][fF]))$
to allow extensions like jPeG etc

Chris Kennedy wrote:
Sorry I'll be more specific, by didn't work I mean it it saw valid (ie
qwerty.jpg) and invalid strings (eg qwerty.doc) as invalid.
"Chris Kennedy" <no****@nospam.co.uk> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
It tried putting this in the validation expression of my regular


expression
validator, got it to validate a textbox with qwerty.jpg as the text value
and it didn't work. Any ideas? Regards, Chris.

"bloomfield" <bl********@as.ro> wrote in message
news:#K*************@TK2MSFTNGP11.phx.gbl...
Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Forma t("{0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Forma t("{0} NOT OK", test));
}
}

Hope this helps

--
____________________
www.bloomfield.as.ro

Chris Kennedy wrote:

Does anyone know a regular expression that will validate the file


extension
but also allow multiple file extensions if necessary. It also needs to


be
case insensitive. Basically, what I want is to validate a file input


box
to
check if the extension is the correct type, i.e. .doc for a Word


Document
etc. Also I would like to check multiple file types, for instance


allow
a
gif or a jpeg or a jpg.

Regards, Chris.




--
____________________
www.bloomfield.as.ro
Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Edwin G. Castro | last post by:
Hi, I'm new to XSLT and I'm having a hard time figuring out whether XSLT will do what I need it to do. I have a XML file with a whole bunch of <message> elements. These <message> elements...
8
by: Rajeev Soni | last post by:
Hi I am looking for the regular expression for validating the allowed file types to upload like files like "zip,pdf,doc,rtf,gif,jpg,png,txt"; and the expression should not be case sensitive like...
2
by: Ori | last post by:
Hi, I'm looking for a good way to validate a US phone number and i though using regular expression for this. I want to support 3 different ways to enter a phone number: 1.Local Phone : 888-8899...
1
by: Chris Kennedy | last post by:
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be case insensitive. Regards, Chris.
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
1
by: vtxr1300 | last post by:
I'm having a problem with a regular expression in conjunction with the regular expression validator. I am trying to make sure that when a user browses for a file to upload, it ends in gif, jpeg or...
2
by: swarnap | last post by:
I have to generate a regular expression dynamically to check the file extensions. The file extension types are stored in a model data which are are to be fetched in form of a string and set into an...
6
by: oscartheduck | last post by:
Hi folks, I have a little script that sits in a directory of images and, when ran, creates thumbnails of the images. It works fine if I call the function inside the program with something like...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.