By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,221 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,221 IT Pros & Developers. It's quick & easy.

Regular Expression to validate file extension

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.