Hello Phil,
I'm not sure you're using this expression correctly.
My expression allows exactly:
some text [a-z]+
a dot \.
some text [a-z]+
The code would look like this:
private bool IsValidFileName(string AFile)
{
Regex rx = new Regex(@"^[a-z]+\.[a-z]+$", RegexOptions.IgnoreCase);
return rx.IsMatch(AFile);
}
The expression you're surrently using seems to allow only names that are
one character that's not from the range you've supplied. Or two or more characters
(any).
Please try to explain as well as possible what you're trying to validate.
Try it like this:
- The first part of the file consists of one or more characters from the
following range: a-z A-Z 0-9 - _
- Followed by an extention which os built up like:
- a dot
- followed by one or more characters from the following range: a-z A-Z
0-9 - _
Having a sound description of the format makes it much easier to actually
write a regex to validate it.
Also, if you're using the same regex more than once, it is best to initialise
it to a static variable and use the RegexOption.Compiled switch like this:
private static Regex _validateFileNameExpression = new Regex(new Regex(@"^[a-z]+\.[a-z]+$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
private bool IsValidFileName(string AFile)
{
return _validateFileNameExpression.IsMatch(AFile);
}
This ensures the regex is only parsed once which makes it much faster on
subsequent calls. Using the compiled switch makes it slower on the first
call as well. So the first call now takes a double performance hit, but all
calls after that are significantly faster.
--
Jesse Houwing
jesse.houwing at sogeti.nl
your example: ^[a-z]+\.[a-z]+$ seems do to exactly the opposite of
what I
need it validates 2 dots (or more).
I was unable to figure out how to reverse it. below is my procedure.
if you
could tell me how to restrict the fielname to just one dot that would
be
great.
private bool IsValidFileName(string AFile)
{
Regex rx = new Regex(@"[~!@#^?{}&*%();<>=|\-|/|\\|\'|'""|\+]");
return !rx.IsMatch(AFile); //Inverted!
}
Phil
"Jesse Houwing" <je***********@newsgroup.nospamwrote in message
news:21**************************@news.microsoft.c om...
>Hello Phil,
>>I am using Regex to validate a file name. I have everything I need
except I
would like the dot(.) in the filename only to appear once. My
question
is it
possible to allow one instance of character but not two or more?
example
myfile.doc = good
My.file.doc = not good
if you could give an example of the expression pattern that would
most
helpful.
thanks
phil
^[a-z]+\.[a-z]+$
Forces just one . in the text.
You could even make the last past optional:
^[a-z]+(\.[a-z]*)?$
If you need more help, please post the regex you've come up with till
now.
--
Jesse Houwing
jesse.houwing at sogeti.nl