Regex expression for numeric value with length check

  • Thread starter Thread starter wdudek
  • Start date Start date
W

wdudek

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();


}
 
wdudek said:
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/library/az24scfc, specifically
"quantifiers". "*" is just a special case. Replace it with "{1,9}" and Bob's
your uncle.
 
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 ([email protected], 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:
(e-mail address removed).

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

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/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


eBob.com said:
I think that this is what you need:
http://msdn.microsoft.com/en-us/library/3206d374.aspx

Also, get a FREE product called Expresso from UltraPico.

Good Luck, Bob


wdudek said:
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,

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 said:
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 ([email protected], 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:
(e-mail address removed).

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

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/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


eBob.com said:
I think that this is what you need:
http://msdn.microsoft.com/en-us/library/3206d374.aspx

Also, get a FREE product called Expresso from UltraPico.

Good Luck, Bob


wdudek said:
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();


}
 
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/micr...af95c193d3ac?hl=en&lnk=st&q=#8c3faf95c193d3ac
(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 said:
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 said:
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 ([email protected], 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:
(e-mail address removed).

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

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/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


eBob.com said:
I think that this is what you need:
http://msdn.microsoft.com/en-us/library/3206d374.aspx

Also, get a FREE product called Expresso from UltraPico.

Good Luck, Bob


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();


}
 
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 said:
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/micr...af95c193d3ac?hl=en&lnk=st&q=#8c3faf95c193d3ac
(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 said:
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 said:
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 ([email protected], 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:
(e-mail address removed).

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

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/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


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

Also, get a FREE product called Expresso from UltraPico.

Good Luck, Bob


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();


}
 
Back
Top