Regular expression to match non-zero zero padding

I need help with a pattern to match zero padding to an integer that doesn't match all zeros. It can have zero to n leading zeros. So far I have:

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

      

but because of the last zero it doesn't get things like 000860

. I feel like it should be easy, but I can't get it. Any help would be much appreciated.

EDIT: Several people have asked which engine / language this is. I thought the regex was standardized so it doesn't matter. But this is .NET.

+3


source to share


5 answers


Why not use this:

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

      



? By the way, you skipped to specify the regex engine to use. But the above pattern should work with just about any regex engine.

+4


source


Just try the following regex:



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

      

+4


source


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

+3


source


Do you want to catch 860?

"000860".match(/^0*([1-9][0-9]*)$/)[1]

      

+1


source


Well here's my trick:

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

it captures at least one zero (add ?

after the first one 0

instead +

if it is possible to have no leading zeros), at least one non-zero number, then any number of zeros and other digits.

and if you want to write a number without zeros:

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

which just puts the part after the leading zeros into the capture group.

play with him here

all this successfully analyzes:

000860
000860123
000800
000810
0008

      

0


source







All Articles