[英]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"
}
所以它允許傳遞一組斜線
這個.
匹配解釋觀察到的行為的每個字符(新行除外)
相反,我們將使用否定的字符類,即[^X]
,這意味着“匹配除了 X之外的所有內容”
根據您的要求,您應該使用:
^((?:[^|<>\\\/?"':*\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.