C# 正则表达式格式未按预期工作

C# 正则表达式格式未按预期工作,c#,regex,string,format,C#,Regex,String,Format,我有以下扩展方法: /* * text.Format("hello", "no") --> Replaces all appearances of "{hello}" with "no" * * For example, if text would have been "{hello} how are you?", the method would have returned "no how are you?" */ public static StringBuilder CustomFo

我有以下扩展方法:

/*
* text.Format("hello", "no") --> Replaces all appearances of "{hello}" with "no"
*
* For example, if text would have been "{hello} how are you?", the method would have returned "no how are you?"
*/
public static StringBuilder CustomFormat(this StringBuilder text, string name, string value)
{
     return text.Replace(String.Format("{{{0}}}", name), value);
}

/*
*  text.FormatUsingRegex("(?'hello'[A-Z][a-z]{3})", "Mamma mia") --> Replaces the text with the found matching group in the input
*
* For example if text would have been "{hello}oth", the method would have returned "Mammoth"
*/
public static StringBuilder FormatUsingRegex(this StringBuilder text, string regexString, string input)
{
     Regex regex = new Regex(regexString);
     List<string> groupNames = regex.GetGroupNames().ToList();
     Match match = regex.Match(input);
     groupNames.ForEach(groupName => text.CustomFormat(groupName, match.Groups[groupName].Value));
     return text;
}
我希望
text
会像这样结束
/index.aspx?xmlFilePath=MCicero_tempreview.xml
,但我得到的是
/index.aspx?xmlFilePath=
,好像组与输入不匹配

我尝试了这个正则表达式并输入,它似乎工作得很好


这里可能发生了什么?

我认为这是因为您在正则表达式末尾使用了
,第一个匹配项是空字符串,因为
表示(在regex101解释之后):

在零到一次之间,尽可能多地返回 需要

即使在regex101示例中,也需要使用/g模式来捕获组,使用/g时,每个字符对之间都有可见的虚线,这意味着regex在那里匹配,因为它总是匹配的。所以函数只返回它捕获的空字符串

因此,请尝试:

(f=(?'xmlFilePath'.*))

@Downvoter,请留下评论,解释投票失败的原因以及我必须采取的措施来改进我的问题
(f=(?'xmlFilePath'.*))