URL 路由参数的正则表达式
regular expression for route parameter of URL
我不太擅长正则表达式,这就是为什么我需要你的帮助。
看这里 http://kohanaframework.org/3.3/guide/kohana/routing#examples:
Route::set('search', ':<query>', array('query' => '.*'))
->defaults(array(
'controller' => 'Search',
'action' => 'index',
));
这个正则表达式 (.*
) 排除了我需要的所有参数:
“cat1/cat2/cat3
”
还有:
"cat1/cat 2/ cat3
",
“cat1/cat 2/ /// a |<>"?\':*
”
如何修改此表达式以禁止:
1. 任何类型的空格 ( "\s
" )
2. 多于一个斜线('cat1/cat2
'但不是'cat1/////cat2
')
3. 范围内的每个符号:[ "|", "<", ">" , "\"", "?", "\", "'", ":", "*"
]
感谢所有试图帮助我的人
define('CATEGORIES_RGXP', '(?:[^|<>\?"\':*\s]+\/?)+');
Route::set('debug_route', '(<categories>/)<file>.<ext>',array(
'categories' => CATEGORIES_RGXP,
))
->defaults(array(
'controller' => 'index',
'action' => 'file',
));
当我跟随“/cat1/cat2/////cat3/file.php”时在控制器中转储:var_dump($this->request->param());
array(3) {
["categories"]=>
string(14) "cat1/cat2/cat3"
["file"]=>
string(4) "file"
["ext"]=>
string(3) "php"
}
所以它允许传递一组几个斜杠
.
匹配解释观察到的行为的每个字符(换行符除外)
相反,我们将使用否定字符 class 即 [^X]
表示 "match everything but X"
根据您的要求,您应该使用:
^((?:[^|<>\\/?"':*\s]+\/?)+)$
DEMO
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
[^|<>\\/?"':*\s any character except: '|', '<', '>',
]+ '\', '\/', '?', '"', ''', ':', '*',
whitespace (\n, \r, \t, \f, and " ")
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\/? '/' (optional (matching the most
amount possible))
--------------------------------------------------------------------------------
)+ end of grouping
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
我不太擅长正则表达式,这就是为什么我需要你的帮助。 看这里 http://kohanaframework.org/3.3/guide/kohana/routing#examples:
Route::set('search', ':<query>', array('query' => '.*'))
->defaults(array(
'controller' => 'Search',
'action' => 'index',
));
这个正则表达式 (.*
) 排除了我需要的所有参数:
“cat1/cat2/cat3
”
还有:
"cat1/cat 2/ cat3
",
“cat1/cat 2/ /// a |<>"?\':*
”
如何修改此表达式以禁止:
1. 任何类型的空格 ( "\s
" )
2. 多于一个斜线('cat1/cat2
'但不是'cat1/////cat2
')
3. 范围内的每个符号:[ "|", "<", ">" , "\"", "?", "\", "'", ":", "*"
]
感谢所有试图帮助我的人
define('CATEGORIES_RGXP', '(?:[^|<>\?"\':*\s]+\/?)+');
Route::set('debug_route', '(<categories>/)<file>.<ext>',array(
'categories' => CATEGORIES_RGXP,
))
->defaults(array(
'controller' => 'index',
'action' => 'file',
));
当我跟随“/cat1/cat2/////cat3/file.php”时在控制器中转储:var_dump($this->request->param());
array(3) {
["categories"]=>
string(14) "cat1/cat2/cat3"
["file"]=>
string(4) "file"
["ext"]=>
string(3) "php"
}
所以它允许传递一组几个斜杠
.
匹配解释观察到的行为的每个字符(换行符除外)
相反,我们将使用否定字符 class 即 [^X]
表示 "match everything but X"
根据您的要求,您应该使用:
^((?:[^|<>\\/?"':*\s]+\/?)+)$
DEMO
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
[^|<>\\/?"':*\s any character except: '|', '<', '>',
]+ '\', '\/', '?', '"', ''', ':', '*',
whitespace (\n, \r, \t, \f, and " ")
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\/? '/' (optional (matching the most
amount possible))
--------------------------------------------------------------------------------
)+ end of grouping
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string