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

前端 未结 5 1808
北荒
北荒 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:54

    Since PCRE supports lookarounds, \d{4}(? will find any instance of four consecutive non-zero characters. See it in action here.

    If you must make sure the match only occurs in the correct position of the string, you can use ^.{X}\d{4}(? instead, where X and Y are the number of preceding and following characters, respectively (12 and 5 in your example.)

提交回复
热议问题