1

我正在寻找一个正则表达式来匹配用'模块PHP PCRE重写的 uri 。uri如下:Apachemod_rewrite

/param1/param2/param3/param4

uri的规则

  • 必须至少包含一个/
  • 参数只能允许字母、数字-_
  • 前两条规则必须有零个或多个实例;
4

2 回答 2

2
/\/[a-zA-Z0-9_\-\/]+$/

我假设它必须以 an 开头,这样的/东西不应该匹配/param1/param2/param3/param4*

于 2012-12-04T07:34:30.510 回答
1

怎么样:

if (preg_match("~^(?:/[\w-]+)+/?$~", $string)) {
    # do stuff
}

解释:

The regular expression:

(?-imsx:^(?:/[\w-]+)+/?$)

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
----------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    /                        '/'
----------------------------------------------------------------------
    [\w-]+                   any character of: word characters (a-z,
                             A-Z, 0-9, _), '-' (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )+                       end of grouping
----------------------------------------------------------------------
  /?                       '/' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2012-12-04T10:24:10.543 回答