Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# - Need help with my regular expression

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 94
SqlDependency to get update from sql to my c# app 2 63
PrintingFoundIt(Almost!(Damn!)) 1 30
How to repeat the data 4 17
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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