Solved

C# - Need help with my regular expression

Posted on 2009-05-20
5
180 Views
Last Modified: 2012-05-07
Hi all,

can someone help me with my regular expression. I have already read many pages about regular expressions, but I just don't seem to get it in general.

The function below need to check if the control_string is positive, contains only numbers and needs tot start with 1-9 followed by zero or more other numbers.

So "1", "12", "230" are ok, but "01", "a", "23c" etc. are wrong (all without the quotes of course). At the moment it works for the most part, but it also accepts "01".

What am I doing wrong?

I hope you guys can help me out.
public static Boolean checkIt(string control_string)

{

    Regex objNotNaturalPattern = new Regex("[^0-9]");

    Regex objNaturalPattern = new Regex("[1-9][0-9]*");
 

    return (!objNotNaturalPattern.IsMatch(control_string) && objNaturalPattern.IsMatch(control_string));
 

}

Open in new window

0
Comment
Question by:fourice
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 24429794
^[1-9][0-9]*$
0
 
LVL 6

Author Comment

by:fourice
ID: 24429821
Thanks, it works!
Is it possible that you explain a bit how I can 'read' this pattern?
0
 
LVL 6

Author Closing Comment

by:fourice
ID: 31583417
Works like a charm, but in my effort to understand regular expressions, I would like to know how to 'read' the pattern.
0
 
LVL 6

Author Comment

by:fourice
ID: 24429842
I forgot to ask, what was wrong with my original pattern?
0
 
LVL 84

Expert Comment

by:ozo
ID: 24429855
 ^                        the beginning of the string
----------------------------------------------------------------------
  [1-9]                    any character of: '1' to '9'
----------------------------------------------------------------------
  [0-9]*                   any character of: '0' to '9' (0 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search by two fields at the same time using c# radio button 5 39
Authentication of Web Services 3 53
Name space syntax error 12 43
VB.Net How to Exit Sub - Exit Form??? 5 51
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now