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?
Regex.Replace(this.Text, "<font color="#(?:[\\d\\w]{6})">", "<font color=\"#$1\">");
it is:
(?:[\\d\\w]{6})
instead of
([\\d\\w]{6})
You can use @ btw to escape all the special chars: @"(?:[\d\w]{6})"
Also, have you tried
"<font color=\"#" + $1 + "\">"
Otherwise I don't think c# will know $1 from an ordinary string value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With