网址路由参数的正则表达式


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