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

Regex and repeating characters

P: n/a
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
Aug 29 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Aug 29 '07 #2

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


Aug 30 '07 #3

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

Aug 30 '07 #4

P: n/a
I see what you are doing now, I think we are after the same thing only I am
trying to exclude certain characters and your expression is limiting to a
set of characters. I see how yours could work; except certain characters are
allowed such as a space or an under score. how would you add them in to
your expression string?
thanks
phil.
"Jesse Houwing" <je***********@newsgroup.nospamwrote in message
news:21**************************@news.microsoft.c om...
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. com...
>>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


Aug 30 '07 #5

P: n/a
I got it!
thanks for all your help.
phil

"Phil Barber" <pb******@houston.rr.comwrote in message
news:OJ**************@TK2MSFTNGP05.phx.gbl...
>I see what you are doing now, I think we are after the same thing only I am
trying to exclude certain characters and your expression is limiting to a
set of characters. I see how yours could work; except certain characters
are allowed such as a space or an under score. how would you add them in
to your expression string?
thanks
phil.
"Jesse Houwing" <je***********@newsgroup.nospamwrote in message
news:21**************************@news.microsoft.c om...
>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 .com...

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



Aug 30 '07 #6

P: n/a
Hello Phil,
I got it!
thanks for all your help.
You're very welcome. You could also change my expression to exclude characters
like this:

[^!@#$%]+

The ^ at the beginning creates the inverse character set.

Jesse

phil
"Phil Barber" <pb******@houston.rr.comwrote in message
news:OJ**************@TK2MSFTNGP05.phx.gbl...
>I see what you are doing now, I think we are after the same thing
only I am
trying to exclude certain characters and your expression is limiting
to a
set of characters. I see how yours could work; except certain
characters
are allowed such as a space or an under score. how would you add
them in
to your expression string?
thanks
phil.
"Jesse Houwing" <je***********@newsgroup.nospamwrote in message
news:21**************************@news.microsoft. com...
>>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.microsof t.com...

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
--
Jesse Houwing
jesse.houwing at sogeti.nl
Aug 30 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.