Regular Expression - 4 digits in a row, but can't be all zeros

前端 未结 5 1806
北荒
北荒 2021-01-04 02:31

I am looking for a solution that can exclusively be done with a regular expression. I know this would be easy with variables, substrings, etc.

And I am looking fo

5条回答
  •  灰色年华
    2021-01-04 02:40

    Since I complained that the some of the answers here weren't regular expressions, I thought I'd best give you a regex answer. This is primitive, there's probably a better way, but it does work:

    ([1-9][0-9][0-9][0-9]|[0-9][1-9][0-9][0-9]|[0-9][0-9][1-9][0-9]|[0-9][0-9][0-9][1-9])
    

    This checks for something which contains 0-9 in each location, except one which must lie in 1-9, preventing 0000 from matching. You can probably write this simpler using \d instead of [0-9] if your regex parser supports that metacharacter.

提交回复
热议问题