6

I need help with a pattern to match a zero-padded integer which is not all zeros. It can have zero to n leading zeros. So far I have:

^[0-9]{0,}[1-9]{1}$"

but this does not get things like 000860 because of the last zero. I feel like this should be simple, but I can't get it. Any help would be much appreciated.

EDIT: A few people have asked which engine/language this is. I thought regex was standardized so it wouldn't matter. But it's .NET.

rory.ap
  • 34,009
  • 10
  • 83
  • 174

5 Answers5

5

Why not using this:

^0*[1-9][0-9]*$

? Btw, you missed to specify the regex engine in use. But the above pattern should work in almost any regex engine.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Why does `0*` work for the first zero(s) but not the last one? In other words, why can't I do this: `^0*[1-9]0*$`? – rory.ap Sep 02 '14 at 20:31
  • Which programming language or regex engine are you using? – hek2mgl Sep 02 '14 at 20:33
  • That will leave you with any number of zeros followed by a single positive integer followed by any number of zeros. You can tweak what you suggested by doing this: ^0*[1-9]*0* but that will leave you with a regex that won't match something with a zero in the middle like this: 00342010. If you replace the ending 0* with \d* or [0-9]* then you should have what you want. – S.C. Sep 02 '14 at 20:34
  • @S.C. Isn't `00100` or `001000` valid? Btw, what is the difference to your answer? – hek2mgl Sep 02 '14 at 20:36
  • Yes, both of those are valid based on my interpretation of the question. If that's not what is wanted, then 0+ should replace 0* in the beginning. – S.C. Sep 02 '14 at 20:38
  • @hek2mgl -- Yes `00100` and `001000` are valid. – rory.ap Sep 02 '14 at 20:39
  • @hek2mgl, the only difference is \d instead of the explicit [0-9] and mine was submitted a few seconds after yours. – S.C. Sep 02 '14 at 20:42
  • Which is more efficient? – rory.ap Sep 02 '14 at 20:43
  • @roryap You can't do `^0*[1-9]0*` because this would mean: `0-x zeros`, `one number from 1 to 9`, `0-x zeros`. This would match only: `0100`, `00020`, `00001` but not `0110` – hek2mgl Sep 02 '14 at 20:43
  • @hek2mgl -- yep, I see that now. – rory.ap Sep 02 '14 at 20:44
  • @roryap, just looked that up and learned something new. http://stackoverflow.com/questions/17194645/d-versus-0-9-in-net-regular-expressions Good question; thanks for asking. – S.C. Sep 02 '14 at 20:45
  • @S.C. It looks like a communication error :). Your comment, the third one, just wasn't addressed to me but to the OP. +1 for your answer! even if it was given a second after mine.. (hihi :) )... – hek2mgl Sep 02 '14 at 20:46
  • Oops, sorry about that. – S.C. Sep 02 '14 at 20:49
  • Well, I wish I could accept multiple answers, but I'll have to go with efficiency :) Thanks to all. – rory.ap Sep 02 '14 at 20:52
  • Good decision! :D thanks! Just one more thing: next time you are having a regex question, you should probably name the regex engine in your question. This is sometimes/mostly important because the syntax differs between them. – hek2mgl Sep 02 '14 at 20:56
4

Just try with following regex:

^0*[1-9][0-9]*$
hsz
  • 148,279
  • 62
  • 259
  • 315
3

What about this regex? ^0*[1-9]\d*$

S.C.
  • 1,152
  • 8
  • 13
1

Would you like to catch 860?

"000860".match(/^0*([1-9][0-9]*)$/)[1]
Miraage
  • 3,334
  • 3
  • 26
  • 43
  • Thanks for your answer, but a little explanation would be good. – rory.ap Sep 02 '14 at 20:29
  • The parentheses are completely useless.. additionally you make assumptions about the language used, added no explantion whatsoever and didn't even provide a real regex, but an expression matching a constant string against a regex and returning the second match, which will throw an index out of range exception (or similar) -1 – Vogel612 Sep 02 '14 at 21:57
  • The regex is actually usable. But! .NET matches the regex to strings: `new Regex("/^0*[1-9][0-9]*$/").Matches("000860");` would thus be correct. This btw returns a result of type `Match`. My point holds, your answer deserves a downvote. – Vogel612 Sep 03 '14 at 06:49
0

Well here's my take:

0+[1-9]+[0-9]{0,}

it captures at least one zero (append ? after the first 0 instead of +, if its possible to have no leading zeroes), at least one non-zero number then any number of zeroes and other digits.

and if you want to capture the number without the zeroes:

0+([1-9]+[0-9]{0,})

which simply places the part after the leading zeroes in a capture group.

play with it here

parses all of these successfully:

000860
000860123
000800
000810
0008
mechalynx
  • 1,306
  • 1
  • 9
  • 24