Search code examples
regexpcreregex-negationregex-lookaroundsregex-group

Any numeric string except the string containing '000'


How to match all numbers except 000. That is,

001234567502344001233400122300 is fine.
0123456750023440012334012230 is fine.
000123456750234400123340012230 is not fine.
001234567502344000123340012230 is not fine.
0012345675023440012334001223000 is not fine.
00123456750234400012334001223000 is not fine.
001002003004005006 is fine.
001 id fine
10 is fine.
01 is fine.
000 is not fine.

Should I use negative Lookaheads or the following technique:

/(()|()|())/g

Solution

  • You may use

    ^(?!\d*000)\d+$
    

    See the regex demo and the Regulex graph:

    enter image description here

    Details

    • ^ - start of the string
    • (?!\d*000) - right after start of string, there cannot be any 0+ digits followed with 000 substring
    • \d+ - 1+ digits
    • $ - end of string.