验证以特定前缀开头的学生ID

时间:2021-11-13 22:44:29

Until now, I have been validating the ID with the following regex:

到目前为止,我一直在使用以下正则表达式验证ID:

's_id' => 'unique:users|required|max:255|regex:/^\d{3}\-\d{2}\-\d{3}$/'

It validates the ID in the ***-**-*** format.

它以*** - ** - ***格式验证ID。

How can I validate the students id having the initial of 131-35-*** only?

如何验证初始值为131-35的学生ID?***?

1 个解决方案

#1


2  

Since \d{3} matches any three digits and \d{2} matches any two digits, your current regex just matches any first 5 digits.

由于\ d {3}匹配任意三位数且\ d {2}匹配任意两位数,因此您当前的正则表达式只匹配前5位数。

If the digits are set, known beforehand, they can be "hard-coded" into the pattern.

如果预先知道了数字,则可以将它们“硬编码”到模式中。

Use

regex:/^131-35-\d{3}$/

Now, only the last 3 digits can be any digits from 0 to 9.

现在,只有最后3位数字可以是0到9之间的任何数字。

#1


2  

Since \d{3} matches any three digits and \d{2} matches any two digits, your current regex just matches any first 5 digits.

由于\ d {3}匹配任意三位数且\ d {2}匹配任意两位数,因此您当前的正则表达式只匹配前5位数。

If the digits are set, known beforehand, they can be "hard-coded" into the pattern.

如果预先知道了数字,则可以将它们“硬编码”到模式中。

Use

regex:/^131-35-\d{3}$/

Now, only the last 3 digits can be any digits from 0 to 9.

现在,只有最后3位数字可以是0到9之间的任何数字。