Php 正则表达式匹配以数字2开头的8个字母数字,其中至少有两个数字
目前,我有以下匹配8位字母数字的正则表达式,我想修改它,使它必须以数字2开头,并且在这8位中至少包含2个数字。我怎样才能做到Php 正则表达式匹配以数字2开头的8个字母数字,其中至少有两个数字,php,regex,preg-match,php-5.3,Php,Regex,Preg Match,Php 5.3,目前,我有以下匹配8位字母数字的正则表达式,我想修改它,使它必须以数字2开头,并且在这8位中至少包含2个数字。我怎样才能做到 preg_match('/[A-Za-z0-9]{8}/', $bio) 以2开头很容易,只需在开头添加: preg_match('/2[A-Za-z0-9]{7}/', $bio) 但是,正则表达式不适合第二个要求——确保至少有2位数字。你可以设计一个正则表达式来检查里面的两个数字,但不能检查长度是8。因此,您可以创建两个单独的正则表达式(一个表示长度,一个表示2位
preg_match('/[A-Za-z0-9]{8}/', $bio)
以2开头很容易,只需在开头添加:
preg_match('/2[A-Za-z0-9]{7}/', $bio)
但是,正则表达式不适合第二个要求——确保至少有2位数字。你可以设计一个正则表达式来检查里面的两个数字,但不能检查长度是8。因此,您可以创建两个单独的正则表达式(一个表示长度,一个表示2位数字),或者逐个字符分析代码中的输入。以2开头很容易,只需将其添加到开头:
preg_match('/2[A-Za-z0-9]{7}/', $bio)
但是,正则表达式不适合第二个要求——确保至少有2位数字。你可以设计一个正则表达式来检查里面的两个数字,但不能检查长度是8。因此,您可以创建两个单独的正则表达式(一个表示长度,一个表示2位数字),或者逐个字符地分析代码中的输入。如何:
/^(?=2.*\d)[a-zA-Z0-9]{8}$/
如果数字2
算作2个所需数字中的一个
/^(?=2.*\d.*\d)[a-zA-Z0-9]{8}$/
如果数字2
不算作2个所需数字中的一个
/^(?=2.*\d.*\d)[a-zA-Z0-9]{8}$/
说明:
The regular expression:
(?-imsx:^(?=2.*\d)[a-zA-Z0-9]{8}$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
2 '2'
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[a-zA-Z0-9]{8} any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (8 times)
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
那么:
/^(?=2.*\d)[a-zA-Z0-9]{8}$/
如果数字2
算作2个所需数字中的一个
/^(?=2.*\d.*\d)[a-zA-Z0-9]{8}$/
如果数字2
不算作2个所需数字中的一个
/^(?=2.*\d.*\d)[a-zA-Z0-9]{8}$/
说明:
The regular expression:
(?-imsx:^(?=2.*\d)[a-zA-Z0-9]{8}$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
2 '2'
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[a-zA-Z0-9]{8} any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (8 times)
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
这将只匹配一个数字的字符串。这将只匹配一个数字的字符串。@rslite:谢谢;我想每个人每天都会学到新东西。@rslite:谢谢;我想每个人每天都能学到新东西。