0
preg_match("/(11|10|12)([*0-9]+)/i", "11*&!@#")

以上是我试过的。

我的要求是总共6个字符。

10****   
102***
1023**
10234*
102345

前两个字符应该是 10 或 11 或 12,其余四个字符应该像上面的模式。

我怎样才能实现它?

4

3 回答 3

7
1[0-2][0-9*]{4}

这应该满足您的要求:

  • 1一开始
  • 然后012
  • 然后是一个数字或*, 四次

编辑

为了避免像你这样的输入,102**5你可以做更复杂的模式:

1[0-2](([*]{4})|([0-9][*]{3})|([0-9]{2}[*]{2})|([0-9]{3}[*])|([0-9]{4}))
于 2012-12-27T07:13:42.167 回答
3

像这样:

#(10|11|12)([0-9]{4})#

输出:

在此处输入图像描述

于 2012-12-27T07:13:35.863 回答
0

怎么样:

^(?=.{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
----------------------------------------------------------------------
于 2012-12-27T13:03:12.580 回答