我正在寻找一个PHP
PCRE
正则表达式来匹配用Apache
的{{1}}模块重写的uri。 uri如下:
mod_rewrite
uri的规则
/param1/param2/param3/param4
; /
和-
; 答案 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
----------------------------------------------------------------------