正则表达式匹配到的内容的命名在各种Web应用框架的路由(Route)部分很常见。
在PHP中,其基本形式如下:
$pattern = '/(?P<year>\d+).(?P<month>\d+).(?P<day>\d+)/'; preg_match($pattern,'... 2012-12-13 ...', $matches); $keys = array_filter(array_keys($matches), 'is_string');
Web框架中路由设置中的进一步简化
$pattern = 'notes/:id'; $pattern = preg_replace('/:(\w+)/', '(?P<\1>[^/]+)',$pattern); # regexp convert preg_match("#$pattern#", '/notes/123/def', $matches);