Regex to match string not ending with pattern

后端 未结 4 1529
北恋
北恋 2020-12-06 11:07

I try to find a regex that matches the string only if the string does not end with at least three \'0\' or more. Intuitively, I tried:

.*[^0]{3,}$

4条回答
  •  情话喂你
    2020-12-06 12:06

    This is one of those things that RegExes aren't that great at, because the string isn't very regular (whatever that means). The only way I could come up with was to give it every possibility.

    .*[^0]..$|.*.[^0].$|.*..[^0]$
    

    which simplifies to

    .*([^0]|[^0].|[^0]..)$
    

    That's fine if you only want strings not ending in three 0s, but strings not ending in ten 0s would be long. But thankfully, this string is a bit more regular than some of these sorts of combinations, and you can simplify it further.

    .*[^0].{0,2}$
    

提交回复
热议问题