正则表达式在mod_rewrite之后匹配uri

时间:2012-12-04 07:29:32

标签: php regex

我正在寻找一个PHP PCRE正则表达式来匹配用Apache的{​​{1}}模块重写的uri。 uri如下:

mod_rewrite

uri的规则

  • 必须至少包含一个/param1/param2/param3/param4 ;
  • params只允许使用字母,数字,/-;
  • 前两个规则必须有零个或多个实例;

2 个答案:

答案 0 :(得分:2)

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

我假设它必须以/开头,这样的内容不应与/param1/param2/param3/param4*匹配

答案 1 :(得分: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
----------------------------------------------------------------------