473,386 Members | 1,775 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,386 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 4174
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill Cohagan | last post by:
I'm looking for help with a regular expression question, so my first question is which newsgroup is the best one to post to? Just in case *this* is the best choice, here's the problem: I'm...
6
by: Joseph | last post by:
I tried to find a script that would filter out repeating characters before saving the string. But no luck so far. For exemple, if a user writes 'haaaaaaaaaaaaaaa', i would like to get "haaa". If...
3
by: David Merrick | last post by:
Hi ! can any of you help ? Since datasheets and forms can happily display calculated fields over 255 characters long, I was surprised to discover that the same when read via a DAO recordset...
2
by: Daniel Billingsley | last post by:
How do I create a string consisting of a specified character repeated a specified number of times? Something like String.Fill('x', 10) to create "xxxxxxxxxx" The best I see would be some hack...
3
by: Rasika WIJAYARATNE | last post by:
({0,1}{1})|({1}{1}) I am trying to validate for numbers between 1-32 which is the valid range using the above regex in a asp:regexpvalidator. However it accepts numbers 1-29 correctly but does...
10
by: abcd | last post by:
I have a regex: ':\\()*' when I do, re.compile(':\\()*') ...I get sre_constants.error: unbalanced parenthesis do i need to escape something else? i see that i have matching parenthesis. ...
7
by: jaylucier | last post by:
Howdy, I'm trying to break an input string into multpile pieces using a series of delimiters that start with an asterisk. Following the asterisk is a mulitple character identifier immediately...
11
by: mauiboy | last post by:
programming newbie. I'm work on a program that post characters into foreground application. everything is working nicely except for repeating characters. I thought using a task timer was the way to...
10
by: BethL | last post by:
Hello, I'm new to Perl and I'm stepping through an online tutorial. I'm trying to count the lines in a file that have strings with double letters in them. This is my code - $file =...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.