[英]regular expression for route parameter of URL
I'm not great with regular expressions, thats why I need your help. 我对正则表达式不太满意,这就是为什么我需要你的帮助。 look here http://kohanaframework.org/3.3/guide/kohana/routing#examples :
看这里http://kohanaframework.org/3.3/guide/kohana/routing#examples :
Route::set('search', ':<query>', array('query' => '.*'))
->defaults(array(
'controller' => 'Search',
'action' => 'index',
));
this regular expression ( .*
) excepts all parameters what I need: 这个正则表达式(
.*
)除了我需要的所有参数:
" cat1/cat2/cat3
" “
cat1/cat2/cat3
”
but also: 但是也:
" cat1/cat 2/ cat3
", “
cat1/cat 2/ cat3
”,
" cat1/cat 2/ /// a |<>"?\\':*
" “
cat1/cat 2/ /// a |<>"?\\':*
“
How to modify this expression to disallow: 如何修改此表达式以禁止:
1. any kind of spaces ( " \\s
" ) 1.任何空间(“
\\s
”)
2. more then one slash together ( ' cat1/cat2
' but not ' cat1/////cat2
') 2.多一个斜线('
cat1/cat2
'但不是' cat1/////cat2
')
3. and each symbol of range : [ "|", "<", ">" , "\\"", "?", "\\", "'", ":", "*"
] 3.和范围的每个符号:[
"|", "<", ">" , "\\"", "?", "\\", "'", ":", "*"
]
Thanks for everyone who try to help me 感谢所有试图帮助我的人
define('CATEGORIES_RGXP', '(?:[^|<>\\?"\':*\s]+\/?)+');
Route::set('debug_route', '(<categories>/)<file>.<ext>',array(
'categories' => CATEGORIES_RGXP,
))
->defaults(array(
'controller' => 'index',
'action' => 'file',
));
Dump in controller when i follow "/cat1/cat2/////cat3/file.php": var_dump($this->request->param());
当我按照“/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"
}
so it allow to pass a group of few slashes 所以它允许传递一组斜线
the .
这个
.
matches every character (except new line) which explains the observed behaviour 匹配解释观察到的行为的每个字符(新行除外)
Instead, we'll use the negated character class ie [^X]
which means "match everything but X" 相反,我们将使用否定的字符类,即
[^X]
,这意味着“匹配除了 X之外的所有内容”
According to your requirements, you should use then: 根据您的要求,您应该使用:
^((?:[^|<>\\\/?"':*\s]+\/?)+)$
DEMO
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
(?: 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 \1
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.