Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Numeric expression?

I have a textbox for numeric inputs. How can I use RegEx and Match class to
check:
1. Intergers (such as "123", " 1235 ", "0034", "+123", "-0123", ...)
2. Decimal numbers (such as "123.4", " 0123.9 ", ".90", "+12.3", "-43.34")
Thanks for any tips!
David Chu
[292 byte] By [David Chu] at [2007-11-9 18:47:23]
# 1 Re: Numeric expression?
I'd suggest something like:

^(+|-)?(\d+|\d+\.\d*)$

Which you can read as:

+ or - zero or one times, followed by either:

one or more digits
or
one or more digits, followed by a decimal point followed by any number of
digits.

--
Visit the C# product team at http://www.csharp.net

This posting is provided "AS IS" with no warranties, and confers no rights.

"David Chu" <chud@hotmail.com> wrote in message
news:3e95b927$1@tnews.web.dev-archive.com...
>
> I have a textbox for numeric inputs. How can I use RegEx and Match class
to
> check:
>
> 1. Intergers (such as "123", " 1235 ", "0034", "+123", "-0123", ...)
> 2. Decimal numbers (such as "123.4", " 0123.9 ", ".90", "+12.3", "-43.34")
>
> Thanks for any tips!
>
> David Chu
Eric Gunnerson at 2007-11-11 22:00:14 >
# 2 Re: Numeric expression?
Great! Thanks. The reason I post this request is that I am still a lit bit
confused by group. The following is an example from Microsoft:

public static void RunTest()
{
int counter;
Match m;
CaptureCollection cc;
GroupCollection gc;

// Look for groupings of "Abc".
Regex r = new Regex("(Abc)+");
// Define the string to search.
m = r.Match("XYZAbcAbcAbcXYZAbcAb");
gc = m.Groups;

// Print the number of groups.
Console.WriteLine("Captured groups = " + gc.Count.ToString());

// Loop through each group.
for (int i=0; i < gc.Count; i++)
{
cc = gc[i].Captures;
counter = cc.Count;

// Print number of captures in this group.
Console.WriteLine("Captures count = " + counter.ToString());

// Loop through each capture in group.
for (int ii = 0; ii < counter; ii++)
{
// Print capture and position.
Console.WriteLine(cc[ii] + " Starts at character " +
cc[ii].Index);
}
}
}

The result is:

Captured groups = 2
Captures count = 1
AbcAbcAbc Starts at character 3
Captures count = 3
Abc Starts at character 3
Abc Starts at character 6
Abc Starts at character 9

Why the last "Abc" in "XYZAbcAbcAbcXYZAbcAb" is not match? It should have
one match like:

Captures count = 1
Abc Starts at character 15

I run the program, but there is no the last match.

"Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote:
>I'd suggest something like:
>
>^(+|-)?(\d+|\d+\.\d*)$
>
>Which you can read as:
>
>+ or - zero or one times, followed by either:
>
>one or more digits
>or
>one or more digits, followed by a decimal point followed by any number of
>digits.
>
David Chu at 2007-11-11 22:01:14 >
# 3 Re: Numeric expression?
Your regex is

(Abc)+

which is read as "Abc", one or more times

The regex engine tries to find the longest match that satisfies the pattern,
and it finds 3 examples. The last one doesn't satisfy the pattern because
there are other characters that aren't "Abc".

To get the alternate behavior, I think you'd need to use a named capture,
and write something like:

((?<Abc>Abc)|.*?)+

which means match Abc or any other characters more than one time. The second
clause should eat up the non-Abc match.

Does that help?

--
Visit the C# product team at http://www.csharp.net

This posting is provided "AS IS" with no warranties, and confers no rights.

"David Chu" <chud@hotmail.com> wrote in message
news:3e97330f$1@tnews.web.dev-archive.com...
>
> Great! Thanks. The reason I post this request is that I am still a lit bit
> confused by group. The following is an example from Microsoft:
>
> public static void RunTest()
> {
> int counter;
> Match m;
> CaptureCollection cc;
> GroupCollection gc;
>
> // Look for groupings of "Abc".
> Regex r = new Regex("(Abc)+");
> // Define the string to search.
> m = r.Match("XYZAbcAbcAbcXYZAbcAb");
> gc = m.Groups;
>
> // Print the number of groups.
> Console.WriteLine("Captured groups = " + gc.Count.ToString());
>
> // Loop through each group.
> for (int i=0; i < gc.Count; i++)
> {
> cc = gc[i].Captures;
> counter = cc.Count;
>
> // Print number of captures in this group.
> Console.WriteLine("Captures count = " + counter.ToString());
>
> // Loop through each capture in group.
> for (int ii = 0; ii < counter; ii++)
> {
> // Print capture and position.
> Console.WriteLine(cc[ii] + " Starts at character " +
> cc[ii].Index);
> }
> }
> }
>
> The result is:
>
> Captured groups = 2
> Captures count = 1
> AbcAbcAbc Starts at character 3
> Captures count = 3
> Abc Starts at character 3
> Abc Starts at character 6
> Abc Starts at character 9
>
> Why the last "Abc" in "XYZAbcAbcAbcXYZAbcAb" is not match? It should have
> one match like:
>
> Captures count = 1
> Abc Starts at character 15
>
> I run the program, but there is no the last match.
>
>
> "Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote:
> >I'd suggest something like:
> >
> >^(+|-)?(\d+|\d+\.\d*)$
> >
> >Which you can read as:
> >
> >+ or - zero or one times, followed by either:
> >
> >one or more digits
> >or
> >one or more digits, followed by a decimal point followed by any number of
> >digits.
> >
>
Eric Gunnerson at 2007-11-11 22:02:18 >
# 4 Re: Numeric expression?
I think the original code is correct. Actually, the code only prints the first
match. If you continue to get the next match, you will get it:

m = m.NextMatch();
if ( m.Success )
{
// print the next match result which is what I expected!
}

"Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote:
>Your regex is
>
>(Abc)+
>
>which is read as "Abc", one or more times
>
>The regex engine tries to find the longest match that satisfies the pattern,
>and it finds 3 examples. The last one doesn't satisfy the pattern because
>there are other characters that aren't "Abc".
>
>To get the alternate behavior, I think you'd need to use a named capture,
>and write something like:
>
>((?<Abc>Abc)|.*?)+
>
>which means match Abc or any other characters more than one time. The second
>clause should eat up the non-Abc match.
>
>Does that help?
>
>--
>Visit the C# product team at http://www.csharp.net
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
>
>"David Chu" <chud@hotmail.com> wrote in message
>news:3e97330f$1@tnews.web.dev-archive.com...
>>
>> Great! Thanks. The reason I post this request is that I am still a lit
bit
>> confused by group. The following is an example from Microsoft:
>>
>> public static void RunTest()
>> {
>> int counter;
>> Match m;
>> CaptureCollection cc;
>> GroupCollection gc;
>>
>> // Look for groupings of "Abc".
>> Regex r = new Regex("(Abc)+");
>> // Define the string to search.
>> m = r.Match("XYZAbcAbcAbcXYZAbcAb");
>> gc = m.Groups;
>>
>> // Print the number of groups.
>> Console.WriteLine("Captured groups = " + gc.Count.ToString());
>>
>> // Loop through each group.
>> for (int i=0; i < gc.Count; i++)
>> {
>> cc = gc[i].Captures;
>> counter = cc.Count;
>>
>> // Print number of captures in this group.
>> Console.WriteLine("Captures count = " + counter.ToString());
>>
>> // Loop through each capture in group.
>> for (int ii = 0; ii < counter; ii++)
>> {
>> // Print capture and position.
>> Console.WriteLine(cc[ii] + " Starts at character " +
>> cc[ii].Index);
>> }
>> }
>> }
>>
>> The result is:
>>
>> Captured groups = 2
>> Captures count = 1
>> AbcAbcAbc Starts at character 3
>> Captures count = 3
>> Abc Starts at character 3
>> Abc Starts at character 6
>> Abc Starts at character 9
>>
>> Why the last "Abc" in "XYZAbcAbcAbcXYZAbcAb" is not match? It should have
>> one match like:
>>
>> Captures count = 1
>> Abc Starts at character 15
>>
>> I run the program, but there is no the last match.
>>
>>
>> "Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote:
>> >I'd suggest something like:
>> >
>> >^(+|-)?(\d+|\d+\.\d*)$
>> >
>> >Which you can read as:
>> >
>> >+ or - zero or one times, followed by either:
>> >
>> >one or more digits
>> >or
>> >one or more digits, followed by a decimal point followed by any number
of
>> >digits.
>> >
>>
>
>
David Chu at 2007-11-11 22:03:16 >
# 5 Re: Numeric expression?
To get all of them at once, you'll need to look at the Captures collection
rather than Groups

--
Visit the C# product team at http://www.csharp.net

This posting is provided "AS IS" with no warranties, and confers no rights.

"David Chu" <chud@hotmail.com> wrote in message
news:3e9ee8e5$1@tnews.web.dev-archive.com...
>
> I think the original code is correct. Actually, the code only prints the
first
> match. If you continue to get the next match, you will get it:
>
> m = m.NextMatch();
> if ( m.Success )
> {
> // print the next match result which is what I expected!
> }
>
> "Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote:
> >Your regex is
> >
> >(Abc)+
> >
> >which is read as "Abc", one or more times
> >
> >The regex engine tries to find the longest match that satisfies the
pattern,
> >and it finds 3 examples. The last one doesn't satisfy the pattern because
> >there are other characters that aren't "Abc".
> >
> >To get the alternate behavior, I think you'd need to use a named capture,
> >and write something like:
> >
> >((?<Abc>Abc)|.*?)+
> >
> >which means match Abc or any other characters more than one time. The
second
> >clause should eat up the non-Abc match.
> >
> >Does that help?
> >
> >--
> >Visit the C# product team at http://www.csharp.net
> >
> >This posting is provided "AS IS" with no warranties, and confers no
rights.
> >
> >"David Chu" <chud@hotmail.com> wrote in message
> >news:3e97330f$1@tnews.web.dev-archive.com...
> >>
> >> Great! Thanks. The reason I post this request is that I am still a lit
> bit
> >> confused by group. The following is an example from Microsoft:
> >>
> >> public static void RunTest()
> >> {
> >> int counter;
> >> Match m;
> >> CaptureCollection cc;
> >> GroupCollection gc;
> >>
> >> // Look for groupings of "Abc".
> >> Regex r = new Regex("(Abc)+");
> >> // Define the string to search.
> >> m = r.Match("XYZAbcAbcAbcXYZAbcAb");
> >> gc = m.Groups;
> >>
> >> // Print the number of groups.
> >> Console.WriteLine("Captured groups = " + gc.Count.ToString());
> >>
> >> // Loop through each group.
> >> for (int i=0; i < gc.Count; i++)
> >> {
> >> cc = gc[i].Captures;
> >> counter = cc.Count;
> >>
> >> // Print number of captures in this group.
> >> Console.WriteLine("Captures count = " + counter.ToString());
> >>
> >> // Loop through each capture in group.
> >> for (int ii = 0; ii < counter; ii++)
> >> {
> >> // Print capture and position.
> >> Console.WriteLine(cc[ii] + " Starts at character " +
> >> cc[ii].Index);
> >> }
> >> }
> >> }
> >>
> >> The result is:
> >>
> >> Captured groups = 2
> >> Captures count = 1
> >> AbcAbcAbc Starts at character 3
> >> Captures count = 3
> >> Abc Starts at character 3
> >> Abc Starts at character 6
> >> Abc Starts at character 9
> >>
> >> Why the last "Abc" in "XYZAbcAbcAbcXYZAbcAb" is not match? It should
have
> >> one match like:
> >>
> >> Captures count = 1
> >> Abc Starts at character 15
> >>
> >> I run the program, but there is no the last match.
> >>
> >>
> >> "Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote:
> >> >I'd suggest something like:
> >> >
> >> >^(+|-)?(\d+|\d+\.\d*)$
> >> >
> >> >Which you can read as:
> >> >
> >> >+ or - zero or one times, followed by either:
> >> >
> >> >one or more digits
> >> >or
> >> >one or more digits, followed by a decimal point followed by any number
> of
> >> >digits.
> >> >
> >>
> >
> >
>
Eric Gunnerson at 2007-11-11 22:04:20 >