Regex Woes

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/
 
L

Luc E. Mistiaen

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

/LM
 
G

Guest

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top