Solved

C# - Need help with my regular expression

Posted on 2009-05-20
5
178 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

13 Experts available now in Live!

Get 1:1 Help Now