[英]Need a regex that makes sure the number starts with 01 02 or 08 and is 10 or 11 digits long
Am having hard time crediting a regex to check if a number starts with 01
, 02
or 08
and is ten or eleven digits long.我很难相信正则表达式来检查数字是否以
01
、 02
或08
开头并且是十位或十一位长。
For example I want numbers formatted like this to pass:例如,我希望像这样格式化的数字通过:
01614125745
02074125475
0845895412
08004569321
and numbers other forms to fail, number like:和数字其他形式失败,数字如:
0798224141
441544122444
0925456754
and so on.等等。 Basically, any number that does not start with
01
02
or 08
.基本上,任何不以
01
02
或08
开头的数字。
The following pattern follows the criteria:以下模式遵循标准:
/^(?=\d{10,11}$)(01|02|08)\d+/ # This pattern can easily be extended
/^(?=\d{10,11}$)0[128]\d{8,9}/ # Same effect
Explanation:解释:
^ Begin of string
(?=\d{10,11}$) Followed by 10 or 11 digits, followed by the end of the string
(01|02|08) 01, 02 or 08
\d+ The remaining digits
Rob W's answer is correct. Rob W 的回答是正确的。 But I'd use a simpler expression like this: (No need for lookahead)
但我会使用一个更简单的表达方式:(不需要前瞻)
/^0[128][0-9]{8,9}$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.