Generates a sub-expression capture group for a route regex, using an optional user-supplied matching pattern.

Parameters

  • string $regex An optional user-supplied match pattern. If a route is defined like `"/{:id:\d+}"`, then the value will be `"\d+"`.
  • string $param The parameter name which the capture group is assigned to, i.e. `'controller'`, `'id'` or `'args'`.
  • string $token The full token representing a matched element in a route template, i.e. `'/{:action}'`, `'/{:path:js|css}'`, or `'.{:type}'`.
  • string $prefix The prefix character that separates the parameter from the other elements of the route. Usually `'.'` or `'/'`.

Returns

string Returns the full route template, with the value of `$token` replaced with a generated regex capture group.

Source

						protected function _regex($regex, $param, $token, $prefix) {
		if ($regex) {
			$this->_subPatterns[$param] = $regex;
		} elseif ($param == 'args') {
			$regex = '.*';
		} else {
			$regex = '[^\/]+';
		}
		$req = $param === 'args' || array_key_exists($param, $this->_params) ? '?' : '';

		if ($prefix === '/') {
			$pattern = "(?:/(?P<{$param}>{$regex}){$req}){$req}";
		} elseif ($prefix === '.') {
			$pattern = "\\.(?P<{$param}>{$regex}){$req}";
		} else {
			$pattern = "(?P<{$param}>{$regex}){$req}";
		}
		return str_replace($token, $pattern, $this->_pattern);
	}