How can I use the first group in Regex.Replace?
I’ve tried using $1 like the documentation said. Also it doesn’t matter if I use grouping with ?: or not…
string text = "<font color="#aa66bb">farbig</font>"
/// this does not work
Regex.Replace(text, "<font color="#(?:[\\d\\w]{6})">", "<font color=\"#$1\">");
// => "<font color=\"#$1\">farbig</font>"
// this works fine though
Regex.Match(text, "<font color="#([\\d\\w]{6})">").Groups[1];
// => aa66bb
So what am I doing wrong here?
Could it be just that you are using a non-capturing group here?
it is:
instead of
You can use @ btw to escape all the special chars:
@"(?:[\d\w]{6})"Also, have you tried
Otherwise I don’t think c# will know $1 from an ordinary string value