[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular Expression Match any Non-Digit

Posted on 2009-05-01
10
Medium Priority
?
1,091 Views
Last Modified: 2012-05-06
I am trying to match any non-digit in a string and have put together the following C# test.  I don't what I am doing wrong because both of my variables are true?  What is wrong with this pattern?
[TestMethod]
public void DigitsOnly()
{
	//Search for non-digits
	bool hasNonDigits_false = Regex.IsMatch(@"12347890", @"[^\d]*");
 
 
	//Search for non-digits
	bool hasNonDigits_true = Regex.IsMatch(@"1234567890 dfdfh", @"[^\d]*");
 
	Assert.IsTrue(
		hasNonDigits_false == false
		&& hasNonDigits_true == true
		);
 
}

Open in new window

0
Comment
Question by:Chiliyago
  • 5
  • 4
10 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24281117
[^\d]* will match 0 or more occurences, that is why it returns true for even empty string.

Try [^\d]+
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24281131
Actually you don't need the * or + at all, you just want

\D or [^\d]
0
 

Author Comment

by:Chiliyago
ID: 24281340
What pattern would insure the string has ONLY digits?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24281648
Multiple options:

1) To match a string of only digits:   ^\d+$
This matchs from start to finish, in this pattern ^ is the begin string anchor and $ is the end anchor. ^ in this case should not be confused with the negation meaning when used inside [^Z] means everything BUT Z. This pattern will not allow whitespace either.

2) To look for at least one non-digit in the string:  \D
This will match the first occurence of a non-digit
0
 

Author Comment

by:Chiliyago
ID: 24281806
I have altered my test some but still getting odd results.  The hasDigitsOnly_true is false!?
[TestMethod]
public void DigitsOnly()
{
	//Search for non-digits
	bool hasDigitsOnly_true = Regex.IsMatch(@"12347890", @"\D");
 
 
	//Search for non-digits
	bool hasNonDigits_true = Regex.IsMatch(@"1234567890 dfdfh", @"\D");
 
	Assert.IsTrue(
		hasDigitsOnly_true == true
		&& hasNonDigits_true == true
		);
 
}

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24281837
//Search for non-digits
bool hasDigitsOnly_true = Regex.IsMatch(@"12347890", @"\D");


You are searching for a non-digit in a string of all digits. It should be false!
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24281868
Have to throw in the obligatory TryParse


bool DigitsOnly (String value)
{
  int number;
  return Int32.TryParse(value, out number);
}

Open in new window

0
 

Author Comment

by:Chiliyago
ID: 24282168
I have begrudgingly implemented that exact code.  But being stubborn, I want to know why my approach is failing as it is the more proper one.
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 1000 total points
ID: 24282201
The regex \D is "non-digit", so when you look for that pattern, it will return TRUE if it matches a non-digit.


Regex.IsMatch(@"12347890", @"\D");   // will return FALSE


Regex.IsMatch(@"1234567890 dfdfh", @"\D");  // will return TRUE, it matches the first space character after the 0
0
 

Author Comment

by:Chiliyago
ID: 24359952
Thanks
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question