我在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;
}
} …
Run Code Online (Sandbox Code Playgroud)