[英]Replace specific part with regex
I want to replace the textarea value with something else, using C# regex. 我想使用C#regex将textarea值替换为其他值。 Right now I have this:
现在我有这个:
Regex regex = new Regex("<textarea.*>(.*)</textarea>");
string s = "<textarea>test</textarea>";
string a = regex.Replace(s, "abc");
Except this prints abc
instead of <textarea>abc</textarea>
. 除了打印
abc
而不是<textarea>abc</textarea>
。 I want to make it as dynamic as possible, 我希望尽可能让它变得动态,
So something like this 所以这样的事情
<textarea rows="20" class="style">test</textarea>
Should become 应该成为
<textarea rows="20" class="style">abc</textarea>
Thanks! 谢谢!
You need to use capture groups and then put them in the output. 您需要使用捕获组,然后将它们放在输出中。 Like this:
像这样:
void Main()
{
Regex regex = new Regex("(<textarea.*>)(.*)(</textarea>)");
string s = "<textarea>test</textarea>";
string a = regex.Replace(s, "$1abc$3");
Console.WriteLine(a);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.