Tin*_*boy 1 regex associative-array d
我在D中编写了一个非常依赖于性能的代码部分.为此,我希望有一个关联数组将我的数据映射到a,Regex以便我以后可以使用它.
当我尝试这样做时,它给了我错误,index is not a type or expression.如何使用此正则表达式作为我的数组键?
编辑:对于代码,这是我在我的课程中要定义的内容:
View[Regex] m_routes;
Run Code Online (Sandbox Code Playgroud)
我希望这样我可以添加如下路线:
void add(string route, View view)
{
auto regex = regex(route.key, [ 'g', 'i' ]);
if (regex in m_routes)
throw new Exception(format(`Route with path, "%s", is already assigned!`, route));
m_routes[regex] = view;
}
Run Code Online (Sandbox Code Playgroud)
这将允许我检查路由的正则表达式,而不必重建每个路由,如下所示:
View check(string resource)
{
foreach (route; m_routes.byKeyValue())
{
auto match = matchAll(resource, route.key);
// If this regex is a match
// return the view
if (!match.empty)
{
return route.value;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,谢谢!
小智 5
似乎std.regex.Regex是一个带有类型参数的别名:
(来自std.regex.package,版本2.071.0中的第289行)
public alias Regex(Char) = std.regex.internal.ir.Regex!(Char);
Run Code Online (Sandbox Code Playgroud)
换句话说,您需要为正则表达式指定char类型.因为string,那是char:
View[Regex!char] m_routes;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
148 次 |
| 最近记录: |