繁体   English   中英

需要帮助使正则表达式

[英]Need help making regex

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开始是1
  • 然后是012
  • 然后一个数字或* ,四次

编辑

为了避免输入类似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
----------------------------------------------------------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM