繁体   English   中英

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

[英]Regex to match uri after mod_rewrite

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

/param1/param2/param3/param4

uri的规则

  • 必须包含至少一个/ ;
  • 参数必须只允许字母,数字, -_ ;
  • 前两个规则必须有零个或多个实例;
/\/[a-zA-Z0-9_\-\/]+$/

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

怎么样:

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
----------------------------------------------------------------------

暂无
暂无

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

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