![](/img/trans.png)
[英]Regex differentiate between first and street c# regex.replace
[英]C# Regex.Replace first group
如何在Regex.Replace中使用第一組?
我試過像文件說的那樣使用$1
。 如果我使用?:
分組也沒關系... ?:
或者不...
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
那么我在這里做錯了什么?
難道只是你在這里使用非捕獲組嗎?
Regex.Replace(this.Text, "<font color="#(?:[\\d\\w]{6})">", "<font color=\"#$1\">");
它是:
(?:[\\d\\w]{6})
代替
([\\d\\w]{6})
您可以使用@ btw來逃避所有特殊字符: @"(?:[\\d\\w]{6})"
還有,你試過嗎?
"<font color=\"#" + $1 + "\">"
否則我不認為c#會從普通的字符串值知道$ 1
這不是回答你問的問題,而是你正試圖在你的例子是什么,你可以使用HtmlDecode
描述這里和避免整個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.