Solved

C# - Need help with my regular expression

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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