Regex.Replace 回调 - 从匹配的组中获取字典键

Regex.Replace callback - get Dictionary key from matched group

我在一个非常小的网站的后端工作,该网站有一个非常基本的模板系统,使用 HTML 评论。它以前在 PHP 上,正在迁移到 .NET。这是在 PHP 中用 preg_replace_callback.

完成的

我需要用字典中的 title 键替换 <!--[var:title]-->

我有加载模板的基本功能

let loadTemplate templateName =
    if templateName |> templateExists then
        templateName
        |> getTemplateFilePath
        |> File.ReadAllText
        |> ReplaceVariables
    else
        "<!--[Missing Template: " + templateName + "]-->"

在哪里

let varReplaceCallback (matchedVar: Match) =
    printfn "%O" matchedVar
    "Hello"
    //Here is where I need help. I need to return a dictionary with key (.*?)

let ReplaceVariables (string:string) =
    Regex.Replace(string, "<!--\[var\:(.*?)\]-->", MatchEvaluator varReplaceCallback)

我本来希望 varReplaceCallback 收到组 (.*?) 但它收到的是完整匹配 <!--[var:whatever]-->

那么 Regex.Replace 在这里使用是正确的吗?

糟糕,超级简单。对匹配类型了解不够

let varReplaceCallback (matchedVar: Match) =
    let varName = matchedVar.Groups.[1] |> string
    values.[varName] //Where values is a map/dictionary