我正在研究D代码中一个非常依赖性能的部分。为此,我希望将一个关联数组映射到一个Regex
,以便稍后使用它。正则表达式作为关联数组的关键?
当我试图做到这一点,它给了我错误,index is not a type or expression
。我怎样才能使用这个正则表达式作为我的数组键?
编辑:对于代码,这里就是我想在我的类定义:
View[Regex] m_routes;
我想要的,这样我可以添加像下面的路线:
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;
}
这然后让我检查一条路线上的正则表达式,而不必重新构建每条路线,如下所示:
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;
}
任何帮助将不胜感激,谢谢!
这将是更容易帮助你,如果你提供显示问题的一些示例代码。 –
@JonathanMDavis为我试图实现的目标添加了一些代码 – Tinfoilboy