[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Regex expression for numeric value with length check

Bill D

10/27/2008 3:55:00 PM

Hi,

I am horrible at regular expressions so I'm hoping this might be an easy
one for someone else. I need to perform a check that a string contains all
numbers and is between 1 and 9 characters long. I have the first part
working, but can't get the length check to work.

This is what I am using to perform the check to see if the string is all
numeric. ^[0-9]*$

However I can't get it to match anything when I have tried to check the
length as well. Also I need to do this in one expression, i.e. I can't make 1
call to check for numeric values and a second to check for length.

Thanks,
Bill

P.S. This is what I have been using to test this.


static void Main(string[] args)
{
//string input = "00767140001575333";
string input = "00767140";
Regex r = new Regex("^[0-9]*$");


bool result = r.IsMatch(input);
Console.WriteLine(result.ToString());

Console.WriteLine("hit a key");
Console.ReadKey();


}
6 Answers

Jeroen Mostert

10/27/2008 6:11:00 PM

0

wdudek wrote:
> I am horrible at regular expressions so I'm hoping this might be an easy
> one for someone else. I need to perform a check that a string contains all
> numbers and is between 1 and 9 characters long.

That's impossible! If the string has to contain all numbers, it's infinitely
long!

Cue geeky laughter.

> I have the first part working, but can't get the length check to work.
>
> This is what I am using to perform the check to see if the string is all
> numeric. ^[0-9]*$
>
> However I can't get it to match anything when I have tried to check the
> length as well.

Look at http://msdn.microsoft.com/librar..., specifically
"quantifiers". "*" is just a special case. Replace it with "{1,9}" and Bob's
your uncle.

--
J.

eBob.com

10/28/2008 12:35:00 AM

0

I think that this is what you need:
http://msdn.microsoft.com/en-us/library/320...

Also, get a FREE product called Expresso from UltraPico.

Good Luck, Bob


"wdudek" <wdudek@newsgroup.nospam> wrote in message
news:EE3DD8A1-CDB3-497B-BF78-64A6E53D0FF3@microsoft.com...
> Hi,
>
> I am horrible at regular expressions so I'm hoping this might be an
> easy
> one for someone else. I need to perform a check that a string contains all
> numbers and is between 1 and 9 characters long. I have the first part
> working, but can't get the length check to work.
>
> This is what I am using to perform the check to see if the string is all
> numeric. ^[0-9]*$
>
> However I can't get it to match anything when I have tried to check the
> length as well. Also I need to do this in one expression, i.e. I can't
> make 1
> call to check for numeric values and a second to check for length.
>
> Thanks,
> Bill
>
> P.S. This is what I have been using to test this.
>
>
> static void Main(string[] args)
> {
> //string input = "00767140001575333";
> string input = "00767140";
> Regex r = new Regex("^[0-9]*$");
>
>
> bool result = r.IsMatch(input);
> Console.WriteLine(result.ToString());
>
> Console.WriteLine("hit a key");
> Console.ReadKey();
>
>
> }


Jialiang Ge [MSFT]

10/28/2008 7:03:00 AM

0

Good morning Wdudek,

Based on the MSDN article mentioned by Bob, I write this regular expression
for your reference. It checks a string contains all numbers and is between
1~9 characters long.

\b([0-9]{1,9})\b

Please let us know whether it works for you or not.

Have a very nice day!

Regards,
Jialiang Ge (jialge@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within 2
business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa9...
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


"eBob.com" <fakename@totallybogus.com> wrote in message
news:uOy%23zTJOJHA.3876@TK2MSFTNGP04.phx.gbl...
>I think that this is what you need:
>http://msdn.microsoft.com/en-us/library/320...
>
> Also, get a FREE product called Expresso from UltraPico.
>
> Good Luck, Bob
>
>
> "wdudek" <wdudek@newsgroup.nospam> wrote in message
> news:EE3DD8A1-CDB3-497B-BF78-64A6E53D0FF3@microsoft.com...
>> Hi,
>>
>> I am horrible at regular expressions so I'm hoping this might be an
>> easy
>> one for someone else. I need to perform a check that a string contains
>> all
>> numbers and is between 1 and 9 characters long. I have the first part
>> working, but can't get the length check to work.
>>
>> This is what I am using to perform the check to see if the string is all
>> numeric. ^[0-9]*$
>>
>> However I can't get it to match anything when I have tried to check the
>> length as well. Also I need to do this in one expression, i.e. I can't
>> make 1
>> call to check for numeric values and a second to check for length.
>>
>> Thanks,
>> Bill
>>
>> P.S. This is what I have been using to test this.
>>
>>
>> static void Main(string[] args)
>> {
>> //string input = "00767140001575333";
>> string input = "00767140";
>> Regex r = new Regex("^[0-9]*$");
>>
>>
>> bool result = r.IsMatch(input);
>> Console.WriteLine(result.ToString());
>>
>> Console.WriteLine("hit a key");
>> Console.ReadKey();
>>
>>
>> }
>
>


Bill D

10/28/2008 1:49:00 PM

0

Jialiang,

I tried the expresssion you provided, but it failed to match what I
would expected to be valid. I have included the sample code below in case I
am doing something wrong. In the mean time I have also downloaded the
expresso tool to try and figure this out on my side. Thanks

Bill

static void Main(string[] args)
{

//string input = "00767140001575333";
string input = "012345678";
//Regex r = new Regex("^[0-9]*$");
Regex r = new Regex("\b([0-9]{1,9})\b");


bool result = r.IsMatch(input);
Console.WriteLine(result.ToString());

Console.WriteLine("hit a key");
Console.ReadKey();


}


"Jialiang Ge [MSFT]" wrote:

> Good morning Wdudek,
>
> Based on the MSDN article mentioned by Bob, I write this regular expression
> for your reference. It checks a string contains all numbers and is between
> 1~9 characters long.
>
> \b([0-9]{1,9})\b
>
> Please let us know whether it works for you or not.
>
> Have a very nice day!
>
> Regards,
> Jialiang Ge (jialge@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....
>
> MSDN Managed Newsgroup support offering is for non-urgent issues where an
> initial response from the community or a Microsoft Support Engineer within 2
> business day is acceptable. Please note that each follow up response may
> take approximately 2 business days as the support professional working with
> you may need further investigation to reach the most efficient resolution.
> The offering is not appropriate for situations that require urgent,
> real-time or phone-based interactions. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/en-us/subscriptions/aa9...
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> "eBob.com" <fakename@totallybogus.com> wrote in message
> news:uOy%23zTJOJHA.3876@TK2MSFTNGP04.phx.gbl...
> >I think that this is what you need:
> >http://msdn.microsoft.com/en-us/library/320...
> >
> > Also, get a FREE product called Expresso from UltraPico.
> >
> > Good Luck, Bob
> >
> >
> > "wdudek" <wdudek@newsgroup.nospam> wrote in message
> > news:EE3DD8A1-CDB3-497B-BF78-64A6E53D0FF3@microsoft.com...
> >> Hi,
> >>
> >> I am horrible at regular expressions so I'm hoping this might be an
> >> easy
> >> one for someone else. I need to perform a check that a string contains
> >> all
> >> numbers and is between 1 and 9 characters long. I have the first part
> >> working, but can't get the length check to work.
> >>
> >> This is what I am using to perform the check to see if the string is all
> >> numeric. ^[0-9]*$
> >>
> >> However I can't get it to match anything when I have tried to check the
> >> length as well. Also I need to do this in one expression, i.e. I can't
> >> make 1
> >> call to check for numeric values and a second to check for length.
> >>
> >> Thanks,
> >> Bill
> >>
> >> P.S. This is what I have been using to test this.
> >>
> >>
> >> static void Main(string[] args)
> >> {
> >> //string input = "00767140001575333";
> >> string input = "00767140";
> >> Regex r = new Regex("^[0-9]*$");
> >>
> >>
> >> bool result = r.IsMatch(input);
> >> Console.WriteLine(result.ToString());
> >>
> >> Console.WriteLine("hit a key");
> >> Console.ReadKey();
> >>
> >>
> >> }
> >
> >
>
>
>

eBob.com

10/28/2008 5:02:00 PM

0

Using Expresso I get a match for Jialiang's expression but using a small
VB.Net program the equivalent of your C code I do not. I suspect that the
reason for the difference is explained here:
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_thread/thread/bcf72b004079cd5a/8c3faf95c193d3ac?hl=en&lnk=st&q=#8c3faf...
(look for kotte's explanation).

The essence of what you need is ([0-9]{1,9}) from Jialiang's expression.
Now I think you have to begin to work with your real data and decide if you
need to use the \b (word boundary) or the beginning of string/line, end of
string/line characters. You may also have to look at the Multiline option.
Is 18 consecutive digits no match or two matches?

Note that \d is the same as [0-9]. And, from what I know about what it is
you are trying to do, you should not need the parentheses. So the essence
of what you need is \d{1,9}

Good Luck, Bob

"wdudek" <wdudek@newsgroup.nospam> wrote in message
news:FFF3E43F-1240-46B0-A068-4D86F2B57791@microsoft.com...
> Jialiang,
>
> I tried the expresssion you provided, but it failed to match what I
> would expected to be valid. I have included the sample code below in case
> I
> am doing something wrong. In the mean time I have also downloaded the
> expresso tool to try and figure this out on my side. Thanks
>
> Bill
>
> static void Main(string[] args)
> {
>
> //string input = "00767140001575333";
> string input = "012345678";
> //Regex r = new Regex("^[0-9]*$");
> Regex r = new Regex("\b([0-9]{1,9})\b");
>
>
> bool result = r.IsMatch(input);
> Console.WriteLine(result.ToString());
>
> Console.WriteLine("hit a key");
> Console.ReadKey();
>
>
> }
>
>
> "Jialiang Ge [MSFT]" wrote:
>
>> Good morning Wdudek,
>>
>> Based on the MSDN article mentioned by Bob, I write this regular
>> expression
>> for your reference. It checks a string contains all numbers and is
>> between
>> 1~9 characters long.
>>
>> \b([0-9]{1,9})\b
>>
>> Please let us know whether it works for you or not.
>>
>> Have a very nice day!
>>
>> Regards,
>> Jialiang Ge (jialge@online.microsoft.com, remove 'online.')
>> Microsoft Online Community Support
>>
>> Delighting our customers is our #1 priority. We welcome your comments and
>> suggestions about how we can improve the support we provide to you.
>> Please
>> feel free to let my manager know what you think of the level of service
>> provided. You can send feedback directly to my manager at:
>> msdnmg@microsoft.com.
>>
>> ==================================================
>> Get notification to my posts through email? Please refer to
>> http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....
>>
>> MSDN Managed Newsgroup support offering is for non-urgent issues where an
>> initial response from the community or a Microsoft Support Engineer
>> within 2
>> business day is acceptable. Please note that each follow up response may
>> take approximately 2 business days as the support professional working
>> with
>> you may need further investigation to reach the most efficient
>> resolution.
>> The offering is not appropriate for situations that require urgent,
>> real-time or phone-based interactions. Issues of this nature are best
>> handled working with a dedicated Microsoft Support Engineer by contacting
>> Microsoft Customer Support Services (CSS) at
>> http://msdn.microsoft.com/en-us/subscriptions/aa9...
>> ==================================================
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>>
>> "eBob.com" <fakename@totallybogus.com> wrote in message
>> news:uOy%23zTJOJHA.3876@TK2MSFTNGP04.phx.gbl...
>> >I think that this is what you need:
>> >http://msdn.microsoft.com/en-us/library/320...
>> >
>> > Also, get a FREE product called Expresso from UltraPico.
>> >
>> > Good Luck, Bob
>> >
>> >
>> > "wdudek" <wdudek@newsgroup.nospam> wrote in message
>> > news:EE3DD8A1-CDB3-497B-BF78-64A6E53D0FF3@microsoft.com...
>> >> Hi,
>> >>
>> >> I am horrible at regular expressions so I'm hoping this might be an
>> >> easy
>> >> one for someone else. I need to perform a check that a string contains
>> >> all
>> >> numbers and is between 1 and 9 characters long. I have the first part
>> >> working, but can't get the length check to work.
>> >>
>> >> This is what I am using to perform the check to see if the string is
>> >> all
>> >> numeric. ^[0-9]*$
>> >>
>> >> However I can't get it to match anything when I have tried to check
>> >> the
>> >> length as well. Also I need to do this in one expression, i.e. I can't
>> >> make 1
>> >> call to check for numeric values and a second to check for length.
>> >>
>> >> Thanks,
>> >> Bill
>> >>
>> >> P.S. This is what I have been using to test this.
>> >>
>> >>
>> >> static void Main(string[] args)
>> >> {
>> >> //string input = "00767140001575333";
>> >> string input = "00767140";
>> >> Regex r = new Regex("^[0-9]*$");
>> >>
>> >>
>> >> bool result = r.IsMatch(input);
>> >> Console.WriteLine(result.ToString());
>> >>
>> >> Console.WriteLine("hit a key");
>> >> Console.ReadKey();
>> >>
>> >>
>> >> }
>> >
>> >
>>
>>
>>


Bill D

10/28/2008 5:41:00 PM

0

Thanks for all the help everyone. From these posts and the other links I was
able to
come up with this ^([0-9]{1,9})$ which did exactly what I wanted. Basically
I want
only numeric values and the string making up those values can only be 9
characters
long. So 18 characters of numeric values would not be valid even though it
containted
2 sets that would be. I also found that this works as well ^(\d{1,9})$.

Thanks
Bill

"eBob.com" wrote:

> Using Expresso I get a match for Jialiang's expression but using a small
> VB.Net program the equivalent of your C code I do not. I suspect that the
> reason for the difference is explained here:
> http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_thread/thread/bcf72b004079cd5a/8c3faf95c193d3ac?hl=en&lnk=st&q=#8c3faf...
> (look for kotte's explanation).
>
> The essence of what you need is ([0-9]{1,9}) from Jialiang's expression.
> Now I think you have to begin to work with your real data and decide if you
> need to use the \b (word boundary) or the beginning of string/line, end of
> string/line characters. You may also have to look at the Multiline option.
> Is 18 consecutive digits no match or two matches?
>
> Note that \d is the same as [0-9]. And, from what I know about what it is
> you are trying to do, you should not need the parentheses. So the essence
> of what you need is \d{1,9}
>
> Good Luck, Bob
>
> "wdudek" <wdudek@newsgroup.nospam> wrote in message
> news:FFF3E43F-1240-46B0-A068-4D86F2B57791@microsoft.com...
> > Jialiang,
> >
> > I tried the expresssion you provided, but it failed to match what I
> > would expected to be valid. I have included the sample code below in case
> > I
> > am doing something wrong. In the mean time I have also downloaded the
> > expresso tool to try and figure this out on my side. Thanks
> >
> > Bill
> >
> > static void Main(string[] args)
> > {
> >
> > //string input = "00767140001575333";
> > string input = "012345678";
> > //Regex r = new Regex("^[0-9]*$");
> > Regex r = new Regex("\b([0-9]{1,9})\b");
> >
> >
> > bool result = r.IsMatch(input);
> > Console.WriteLine(result.ToString());
> >
> > Console.WriteLine("hit a key");
> > Console.ReadKey();
> >
> >
> > }
> >
> >
> > "Jialiang Ge [MSFT]" wrote:
> >
> >> Good morning Wdudek,
> >>
> >> Based on the MSDN article mentioned by Bob, I write this regular
> >> expression
> >> for your reference. It checks a string contains all numbers and is
> >> between
> >> 1~9 characters long.
> >>
> >> \b([0-9]{1,9})\b
> >>
> >> Please let us know whether it works for you or not.
> >>
> >> Have a very nice day!
> >>
> >> Regards,
> >> Jialiang Ge (jialge@online.microsoft.com, remove 'online.')
> >> Microsoft Online Community Support
> >>
> >> Delighting our customers is our #1 priority. We welcome your comments and
> >> suggestions about how we can improve the support we provide to you.
> >> Please
> >> feel free to let my manager know what you think of the level of service
> >> provided. You can send feedback directly to my manager at:
> >> msdnmg@microsoft.com.
> >>
> >> ==================================================
> >> Get notification to my posts through email? Please refer to
> >> http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....
> >>
> >> MSDN Managed Newsgroup support offering is for non-urgent issues where an
> >> initial response from the community or a Microsoft Support Engineer
> >> within 2
> >> business day is acceptable. Please note that each follow up response may
> >> take approximately 2 business days as the support professional working
> >> with
> >> you may need further investigation to reach the most efficient
> >> resolution.
> >> The offering is not appropriate for situations that require urgent,
> >> real-time or phone-based interactions. Issues of this nature are best
> >> handled working with a dedicated Microsoft Support Engineer by contacting
> >> Microsoft Customer Support Services (CSS) at
> >> http://msdn.microsoft.com/en-us/subscriptions/aa9...
> >> ==================================================
> >> This posting is provided "AS IS" with no warranties, and confers no
> >> rights.
> >>
> >>
> >> "eBob.com" <fakename@totallybogus.com> wrote in message
> >> news:uOy%23zTJOJHA.3876@TK2MSFTNGP04.phx.gbl...
> >> >I think that this is what you need:
> >> >http://msdn.microsoft.com/en-us/library/320...
> >> >
> >> > Also, get a FREE product called Expresso from UltraPico.
> >> >
> >> > Good Luck, Bob
> >> >
> >> >
> >> > "wdudek" <wdudek@newsgroup.nospam> wrote in message
> >> > news:EE3DD8A1-CDB3-497B-BF78-64A6E53D0FF3@microsoft.com...
> >> >> Hi,
> >> >>
> >> >> I am horrible at regular expressions so I'm hoping this might be an
> >> >> easy
> >> >> one for someone else. I need to perform a check that a string contains
> >> >> all
> >> >> numbers and is between 1 and 9 characters long. I have the first part
> >> >> working, but can't get the length check to work.
> >> >>
> >> >> This is what I am using to perform the check to see if the string is
> >> >> all
> >> >> numeric. ^[0-9]*$
> >> >>
> >> >> However I can't get it to match anything when I have tried to check
> >> >> the
> >> >> length as well. Also I need to do this in one expression, i.e. I can't
> >> >> make 1
> >> >> call to check for numeric values and a second to check for length.
> >> >>
> >> >> Thanks,
> >> >> Bill
> >> >>
> >> >> P.S. This is what I have been using to test this.
> >> >>
> >> >>
> >> >> static void Main(string[] args)
> >> >> {
> >> >> //string input = "00767140001575333";
> >> >> string input = "00767140";
> >> >> Regex r = new Regex("^[0-9]*$");
> >> >>
> >> >>
> >> >> bool result = r.IsMatch(input);
> >> >> Console.WriteLine(result.ToString());
> >> >>
> >> >> Console.WriteLine("hit a key");
> >> >> Console.ReadKey();
> >> >>
> >> >>
> >> >> }
> >> >
> >> >
> >>
> >>
> >>
>
>
>