Hi,
Can I ask for a pointer re C# and Regex. I've got the routine that works ok below that finds links within CSS. If I wanted to rewrite the links I find as I go through, and then have a copy of a string at the end of this that represents the initial CSS text but with the rewritten links in place how would I do this?
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
thanks
PS. The catch is I need to be able to pass the URL segment I find (i.e. in capture group 2, for which in "Regex.Replace" I would refer to as $2), to a function to work out the replacement string. I don't seem to be able to do this within with this approach:
Regex.Replace(cssText, regexStr, @"url($1" + fn("$2") + @"$3)") //DOES NOT SEEM TO WORK
Any ideas?