473,686 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4206
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(AFi le); //Inverted!

}

Phil

"Jesse Houwing" <je***********@ newsgroup.nospa mwrote in message
news:21******** *************** ***@news.micros oft.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 #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.Ig noreCase);
return rx.IsMatch(AFil e);
}

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.Com piled switch like this:

private static Regex _validateFileNa meExpression = new Regex(new Regex(@"^[a-z]+\.[a-z]+$",
RegexOptions.Ig noreCase | RegexOptions.Co mpiled);
private bool IsValidFileName (string AFile)
{
return _validateFileNa meExpression.Is Match(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(AFi le); //Inverted!

}

Phil

"Jesse Houwing" <je***********@ newsgroup.nospa mwrote in message
news:21******** *************** ***@news.micros oft.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.houwin g 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.nospa mwrote in message
news:21******** *************** ***@news.micros oft.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.Ig noreCase);
return rx.IsMatch(AFil e);
}

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.Com piled switch
like this:

private static Regex _validateFileNa meExpression = new Regex(new
Regex(@"^[a-z]+\.[a-z]+$", RegexOptions.Ig noreCase |
RegexOptions.Co mpiled);
private bool IsValidFileName (string AFile)
{
return _validateFileNa meExpression.Is Match(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(AFi le); //Inverted!

}

Phil

"Jesse Houwing" <je***********@ newsgroup.nospa mwrote in message
news:21******* *************** ****@news.micro soft.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.do c = 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.houwi ng at sogeti.nl


Aug 30 '07 #5
I got it!
thanks for all your help.
phil

"Phil Barber" <pb******@houst on.rr.comwrote in message
news:OJ******** ******@TK2MSFTN GP05.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.nospa mwrote in message
news:21******** *************** ***@news.micros oft.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.Ig noreCase);
return rx.IsMatch(AFil e);
}

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.Com piled
switch like this:

private static Regex _validateFileNa meExpression = new Regex(new
Regex(@"^[a-z]+\.[a-z]+$", RegexOptions.Ig noreCase |
RegexOptions.C ompiled);
private bool IsValidFileName (string AFile)
{
return _validateFileNa meExpression.Is Match(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.houwin g 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(AFi le); //Inverted!

}

Phil

"Jesse Houwing" <je***********@ newsgroup.nospa mwrote in message
news:21****** *************** *****@news.micr osoft.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.do c = good
My.file.d oc = 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.houwin g 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******@houst on.rr.comwrote in message
news:OJ******** ******@TK2MSFTN GP05.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.nospa mwrote in message
news:21******* *************** ****@news.micro soft.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.Ig noreCase);
return rx.IsMatch(AFil e);
}
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.Com piled
switch like this:

private static Regex _validateFileNa meExpression = new Regex(new
Regex(@"^[a-z]+\.[a-z]+$", RegexOptions.Ig noreCase |
RegexOptions. Compiled);
private bool IsValidFileName (string AFile)
{
return _validateFileNa meExpression.Is Match(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.houwi ng 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(AFi le); //Inverted!

}

Phil

"Jesse Houwing" <je***********@ newsgroup.nospa mwrote in message
news:21***** *************** ******@news.mic rosoft.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.d oc = good
>My.file.do c = 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.houwi ng 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
1544
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 trying to "parse" something that looks like a command line; e.g., op arg1, arg2, ..., argn The individual parts (op, arg1, ...) can be matched with a \w+ pattern -- except that the args *might* be quoted to cover the case where they contain
6
3259
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 he writes "!!!!!!!!!!!!!", i would like "!!!"... Any suggestions? Thanks
3
6948
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 beyond 255 characters is replaced with garbage. I'd be interested in how many of you also experience this problem. To see if you have, try one of the follow examples. Example ----------
2
41931
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 using PadLeft() or (probably better) some method written from scratch.
3
1246
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 not accept 30-32 (this is meant to be handled by the part of the regex after the pipe | char). Does anyone know why this does not work? Rasika.
10
3388
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. thx
7
5579
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 followed by a data string of variable length. The input string may contain more than one identifier anywhere in the string. Here is an example: *CZ1 2.3 4-56 *fuuuS24364 08 23 72
11
2371
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 go but it seems that as the JVM cleans its garbage the time tries to catchup therefore posting more cycles in it preset duty cycle. any hints on using java to control repeating characters or tasks?
10
4836
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 = 'c:\Perl\Beth\f7.txt'; open(INFO, $file); $i = 1; while ($line = <INFO>) { $_ = $line;
0
8586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8942
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8781
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4317
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1943
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.