需要正则表达式 - 禁止全零 [英] Need a regular expression - disallow all zeros

查看:41
本文介绍了需要正则表达式 - 禁止全零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证一个字符串是否满足以下条件:

I want to validate a string to meet the following conditions:

  • 长度必须为 6 个字符
  • 只有第一个字符可以是字母数字,其余必须是数字
  • 如果第一个数字是字母,则必须是大写
  • 不能全为零

我有以下正则表达式,可以获取除全零部分之外的所有内容.有没有办法禁止全零?

I have the following regular expression that gets everything except the all zeros part. Is there a way to disallow all zeros?

^[A-Z0-9][0-9]{5}$

这是检查正则表达式的唯一方法(并允许000000"),然后专门检查它不是000000"吗?

Is the only way to do this to check the regex (and allow "000000") but then check specifically that it's not "000000"?

谢谢.

推荐答案

只要有一个像这样的否定前瞻来禁止全 0:

Just have a negative lookahead like this to disallow all 0s:

/^(?!0{6})[A-Z0-9][0-9]{5}$/

这篇关于需要正则表达式 - 禁止全零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆