我正在寻找一个正则表达式来匹配用'模块PHP
PCRE
重写的 uri 。uri如下:Apache
mod_rewrite
/param1/param2/param3/param4
uri的规则
- 必须至少包含一个
/
; - 参数只能允许字母、数字
-
和_
; - 前两条规则必须有零个或多个实例;
/\/[a-zA-Z0-9_\-\/]+$/
我假设它必须以 an 开头,这样的/
东西不应该匹配/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
----------------------------------------------------------------------