Regex Woes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm attempting to check a string and see if it has any other characters
besides numeric. Using the following pattern "[^0-9]*", I figured it would
find a successfull non-numeric character if it was matched against a string
and it does just fine. My question/problem is that when I match the pattern
against a string simply like "3", even though there aren't any non-numeric
characters present, it still reports success.

I wrote this pattern using Expresso (http://www.ultrapico.com) and when
using Expresso, the pattern behaves as expected (reports success when
non-numeric char's are presents and doesn't report success when only numeric
char's are present). However when using the following snippet:

Regex ex = new Regex(@"\D*"); // or @"[^0-9]*"
if (ex.Match("3").Success)
Console.WriteLine("I'm busted");

It seems to find a match even though I'm explicitly telling it to match
non-numeric characters. Any ideas? Thanks!

- James
http://www.dotnetwookie.com/blog/hamaze/
 
You may try with "[^0-9]+" because your pattern match also zero non-numeric
characters...

/LM
 
Doh, I'm such a tool, thanks Luc!

- James

Luc E. Mistiaen said:
You may try with "[^0-9]+" because your pattern match also zero non-numeric
characters...

/LM

James said:
I'm attempting to check a string and see if it has any other characters
besides numeric. Using the following pattern "[^0-9]*", I figured it would
find a successfull non-numeric character if it was matched against a
string
and it does just fine. My question/problem is that when I match the
pattern
against a string simply like "3", even though there aren't any non-numeric
characters present, it still reports success.

I wrote this pattern using Expresso (http://www.ultrapico.com) and when
using Expresso, the pattern behaves as expected (reports success when
non-numeric char's are presents and doesn't report success when only
numeric
char's are present). However when using the following snippet:

Regex ex = new Regex(@"\D*"); // or @"[^0-9]*"
if (ex.Match("3").Success)
Console.WriteLine("I'm busted");

It seems to find a match even though I'm explicitly telling it to match
non-numeric characters. Any ideas? Thanks!

- James
http://www.dotnetwookie.com/blog/hamaze/
 
Back
Top