-2

I have the following rule:

Don't allow numbers of any sequence with 4 or more 0s at the beginning.

So this number:

0000438967

will fail validation (4 zeros)

But this number:

0004389678 (3 zeros)

Will pass validation.

the reason for this question was I needed verification on whether I was doing it correctly as, when checking in online Regex validators, I was getting mixed verification results. I know it sounds like a simple question but verification from others is important.

thanks Russ

RuSs
  • 1,725
  • 1
  • 29
  • 47
  • And what is your question regarding your attempt at implementing a solution? – Blorgbeard Jun 26 '15 at 02:24
  • @ShellFish: No mention of look-ahead, not a dup. – nhahtdh Jun 26 '15 at 02:48
  • It's not clear how you want to handle cases like `0`, `0123`, `1`, `000`. Your question is unclear as is. – nhahtdh Jun 26 '15 at 03:14
  • @nhahtdh: Seems pretty clear to me. 0, 0123, 1, 000 are all valid as none of them have 4 or more zeroes at the beginning. – Kaiting Chen Jun 26 '15 at 10:55
  • @KaitingChen: Look at the rest of the answers below. Some of them don't work for those cases. It's easier to downvote those answer with a clearer problem statement. – nhahtdh Jun 26 '15 at 11:15

6 Answers6

3

If you must use regular expression, you can use a Negative Lookahead assertion.

^(?!0000)\d+$

Lookarounds are zero-width assertions. They don't consume any characters on the string. The point of zero-width is the validation to see if a regex can or cannot be matched looking ahead or looking back from the current position, without adding them to the overall match.

Example:

Regex.IsMatch("0438967",      @"^(?!0000)\d+$"); // True
Regex.IsMatch("004389678",    @"^(?!0000)\d+$"); // True
Regex.IsMatch("000438967",    @"^(?!0000)\d+$"); // True
Regex.IsMatch("00004389678",  @"^(?!0000)\d+$"); // False
Regex.IsMatch("00000438967",  @"^(?!0000)\d+$"); // False
Regex.IsMatch("000000438967", @"^(?!0000)\d+$"); // False
hwnd
  • 69,796
  • 4
  • 95
  • 132
3

Zero to three zeroes followed optionally by a nonzero number followed by any number of numbers:

0{0,3}([1-9][0-9]*)?

It should accept:

000

While rejecting any number with 4+ zeroes at the beginning.

Don't use a lookahead/behind for this as that triggers backtracking.

EDIT: if your matching function does not perform an exact match then you'll need to do:

^0{0,3}([1-9][0-9]*)?$
Kaiting Chen
  • 1,076
  • 1
  • 7
  • 12
3

Unbelievable amount of trial & error answers on this question! The solution is as simple as this:

string number = "0000438967";
bool pass = Regex.IsMatch(number, @"^0{0,3}[1-9]\d*$");

This allows numbers of any length, as long as there is at least one non-zero digit. Replace the last asterisk by {n} or {m,n} to also check length.

Of course, you may want to construct a Regex instance for repeated use.

If you also need the numeric value, you can immediately parse it if pass == true:

int value = Int32.Parse(number);

Of course, depending on the maximum length you want to allow you might need an Int64... but anyway you must provide a maximum length in the regex or the Parse can cause an overflow.

Kay Zed
  • 1,304
  • 2
  • 21
  • 31
  • Thanks Kay, Yes, I agree, lots of trial and error. Your answer works and you took the time to explain yourself. – RuSs Jun 28 '15 at 21:15
  • @RuSs With trial and error I referred to the number of answers here in such a short time. You're welcome. – Kay Zed Jul 01 '15 at 03:08
2

Please try this..

        string myString = "00011111";//Valid

        if (Regex.IsMatch(myString,  @"^[0-9]{3}[1-9]+$"))
        {
            Console.WriteLine("Valid");
            Console.ReadKey();
        }
0

Assuming you accept no zeros at all and at least one non-zero digit, here is your regex:

string str = "001";
bool passed = Regex.IsMatch(str, @"^0{0,3}[1-9]\d*$");

Explanation: Regex check for 0 to 3 '0's, then for one non-'0' digits and then for zero or more of any digits.

if "000" is acceptable, then negative look-ahead solution from @hwnd is more elegant. I would just use 0{4} instead of 0000.

Alexey
  • 1,299
  • 11
  • 10
-2

You can use Regex to find out the string is valid or not

!Regex.Match(str, @"^0000(\d+)", RegexOptions.IgnoreCase).Success

here is working sample rextester

Mahendran
  • 468
  • 8
  • 19