472,145 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Regex and repeating characters

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
6 4060
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
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
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
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
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
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.

Similar topics

5 posts views Thread by Bill Cohagan | last post: by
6 posts views Thread by Joseph | last post: by
3 posts views Thread by David Merrick | last post: by
2 posts views Thread by Daniel Billingsley | last post: by
3 posts views Thread by Rasika WIJAYARATNE | last post: by
10 posts views Thread by abcd | last post: by
7 posts views Thread by jaylucier | last post: by
reply views Thread by Saiars | last post: by

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.