Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex问题,如何更新此C#RegexMatches例程以更新/替换找到的项?_C#_Regex_Replace - Fatal编程技术网

Regex问题,如何更新此C#RegexMatches例程以更新/替换找到的项?

Regex问题,如何更新此C#RegexMatches例程以更新/替换找到的项?,c#,regex,replace,C#,Regex,Replace,我可以要一个指针re C#和Regex吗。下面我有一个例程,它可以在CSS中找到链接。如果我想重写我在浏览时找到的链接,然后在末尾有一个字符串的副本,该字符串表示初始CSS文本,但是重写的链接已经准备好了,我该怎么做呢 var resultList = new List<Uri>(); string cssText = new WebClient().DownloadString(uri.ToString()); MatchCollection matches

我可以要一个指针re C#和Regex吗。下面我有一个例程,它可以在CSS中找到链接。如果我想重写我在浏览时找到的链接,然后在末尾有一个字符串的副本,该字符串表示初始CSS文本,但是重写的链接已经准备好了,我该怎么做呢

    var resultList = new List<Uri>();
    string cssText = new WebClient().DownloadString(uri.ToString());
    MatchCollection matches = Regex.Matches(cssText, @"url\(('|"")?([^']*?)('|"")?\)", RegexOptions.IgnoreCase);
    foreach (Match match in matches)
    {
        var groups = match.Groups;
        var relUrl = groups[2].ToString();
        var itemUri = new Uri(uri, relUrl);
        // WANT TO CHANGE / REWRITE THE URI HERE 
        resultList.Add(itemUri);
    }
    // WANT TO HAVE ACCESS TO AN UPDATED "cssText" HERE THAT INCLUDES THE REWRITTEN LINKS

有什么想法吗?

使用Regex.Replace,它可能是这样的

cssText = Regex.Replace(cssText, @"(url\(['""]?)(.*?)(['""]?\))", "$1"+uri+"$2$3");

我不确定变量uri中的值是什么,虽然

不太有效-我需要能够调用一个函数来计算出我想要使用的替换字符串,似乎我不能将此函数放在“Regex.Replace”中调用并使用“$2”作为函数的参数-有什么想法吗?或者可以使用委托,调用fn()其中,
cssText=Regex.Replace(cssText,regexStr,委托(匹配m){返回m.Groups[1].Value+fn(m.Groups[2].Value)+m.Groups[3].Value;})
或在C#3.0>中,可以使用Lambda,
cssText=Regex.Replace(cssText,regexStr,m=>m.Value[1]+fn(m.Value[2])+m.Value[3]),我现在没有C#3.0>,所以Lambda代码没有经过测试。“似乎不工作”?以什么方式?
cssText = Regex.Replace(cssText, @"(url\(['""]?)(.*?)(['""]?\))", "$1"+uri+"$2$3");