正则表达式仅匹配 10 或 12 位数字 [英] Regex to match 10 or 12 digits only

查看:396
本文介绍了正则表达式仅匹配 10 或 12 位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个正则表达式来匹配 10 位或 12 位数字组合.喜欢:

I tried to write a regex to match a 10 or 12 digits number combination. like:

1234567890 - 真
123456789012 - 真
12345678901 - 错误
123456- 错误

1234567890 - True
123456789012 - True
12345678901 - False
123456- False

1234567890123- 错误

1234567890123- False

仅匹配 10 位或 12 位数字.我试过这个:

Only match either 10 or 12 digits. I tried this:

"^[0-9]{10}|[0-9]{12}$"

推荐答案

离你很近了!

这是您要查找的正则表达式:^(\d{10}|\d{12})$.它检查数字(使用 \d).其余的或多或少是您的代码,括号除外.它捕获每个组.如果你想在没有它的情况下工作,你可以松开它们!

This is the regex you're looking for: ^(\d{10}|\d{12})$. It checks for digits (with \d). The rest is more or less your code, with the exception of the parenthesis. It captures each group. You could loose those, if you want to work without it!

在此处查看操作

这篇关于正则表达式仅匹配 10 或 12 位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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