preg_match("/(11|10|12)([*0-9]+)/i", "11*&!@#")
以上是我试过的。
我的要求是总共6个字符。
10****
102***
1023**
10234*
102345
前两个字符应该是 10 或 11 或 12,其余四个字符应该像上面的模式。
我怎样才能实现它?
1[0-2][0-9*]{4}
这应该满足您的要求:
1
一开始0
或1
或2
*
, 四次编辑
为了避免像你这样的输入,102**5
你可以做更复杂的模式:
1[0-2](([*]{4})|([0-9][*]{3})|([0-9]{2}[*]{2})|([0-9]{3}[*])|([0-9]{4}))
像这样:
#(10|11|12)([0-9]{4})#
输出:
怎么样:
^(?=.{6})1[0-2]\d{0,4}\**$
这将匹配您的所有示例,而不是匹配字符串,例如:
1*2*3*
解释:
The regular expression:
(?-imsx:^(?=.{6})1[0-2]\d{0,4}\**$)
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:
----------------------------------------------------------------------
.{6} any character except \n (6 times)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
1 '1'
----------------------------------------------------------------------
[0-2] any character of: '0' to '2'
----------------------------------------------------------------------
\d{0,4} digits (0-9) (between 0 and 4 times
(matching the most amount possible))
----------------------------------------------------------------------
\** '*' (0 or more times (matching the most
amount possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------