首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Regex -不小于或大于等于6位或9位的表达式

Regex -不小于或大于等于6位或9位的表达式
EN

Stack Overflow用户
提问于 2019-06-12 23:19:00
回答 3查看 127关注 0票数 1

我必须编写一个检测6位或9位的正则表达式--意思是:

代码语言:javascript
复制
ID 123 - it should not meet the regext as it is 3 digits
ID 1234 - it should not meet the regext as it is 4 digits
ID 12345 - it should not meet the regext as it is 5 digits
ID 123456 - it should meet the regex it is 6 digits <==========
ID 1234567 - it should not meet the regext as it is 7 digits
ID 12345678 - it should not meet the regext as it is 8 digits
ID 123456789 - it should meet the regext as it is 9 digits <==========
ID 1234567890 - it should not meet the regext as it is 10 digits
ID 12345678901 - it should not meet the regext as it is 11 digits

以此类推。你想明白了吗?

有没有人能帮我做一下这个正则表达式?

EN

回答 3

Stack Overflow用户

发布于 2019-06-12 23:23:19

您可以尝试使用此模式:

代码语言:javascript
复制
\b(\d{6}|\d{9})\b
票数 3
EN

Stack Overflow用户

发布于 2019-06-12 23:47:00

或者另一个:

代码语言:javascript
复制
\b\d{6}(?:\d{3})?\b

See demo at regex101

票数 2
EN

Stack Overflow用户

发布于 2019-06-13 01:04:45

如果要匹配ID部分,则可以使用替换来匹配6位或9位数字。

使用锚点断言字符串的起始^和结束$,或者将单词边界\b添加到模式的开头和结尾。

数字在第一个捕获组中。

代码语言:javascript
复制
^ID (\d{6}|\d{9})$
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56565553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档