Solved

C# - Need help with my regular expression

Posted on 2009-05-20
5
184 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
[X]
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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

752 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