C# 如何正确替换字符串
在我的代码中,我找到所有匹配的元素,并用特殊值替换它C# 如何正确替换字符串,c#,C#,在我的代码中,我找到所有匹配的元素,并用特殊值替换它 Regex imgRule = new Regex("img id=\\\".+?\\\""); MatchCollection matches = imgRule.Matches(content.Value); string result = null; foreach (Match match in matches)
Regex imgRule = new Regex("img id=\\\".+?\\\"");
MatchCollection matches = imgRule.Matches(content.Value);
string result = null;
foreach (Match match in matches)
result = match.Value;
if (result != null)
{
var firstOrDefault = node.ListImages.FirstOrDefault();
if (firstOrDefault != null)
{
var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId));
node.Content = htmlWithImages;
}
}
但是,我的代码是错误的,因为如果有多个匹配项,它将仅替换最后一个匹配项,我如何更正我的代码以替换文本中的所有匹配项?我认为您可能缺少循环中的一组括号 只有这条线是循环的。这就是为什么代码只更新最后一个条目,因为结果设置为集合中的最后一项(在foreach的最后一次迭代中) 已更正的代码
Regex imgRule = new Regex("img id=\\\".+?\\\"");
MatchCollection matches = imgRule.Matches(content.Value);
string result = null;
foreach (Match match in matches) {
result = match.Value;
if (result != null)
{
var firstOrDefault = node.ListImages.FirstOrDefault();
if (firstOrDefault != null)
{
var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId));
node.Content = htmlWithImages;
}
}
}
for循环体周围缺少花括号。如果没有花括号,唯一执行多次的行就是第一行 请尝试以下方法:
foreach (Match match in matches)
{ // added curly brace here
result = match.Value;
if (result != null)
{
var firstOrDefault = node.ListImages.FirstOrDefault();
if (firstOrDefault != null)
{
var htmlWithImages = content.Value.Replace(result,
string.Format("img src='{0}' class='newsimage' width='300'",
firstOrDefault.ImageUrlId));
node.Content = htmlWithImages;
}
}
} // added curly brace here
我还要补充两点:
- 有一个名为
的方法,您可以使用它来代替先使用Regex查找要替换的字符串,然后使用Regex.Replace
string.Replace
- 如果您试图解析HTML,最好使用HTML解析器。看看这是否是一个更容易解决问题的方法
Regex.Replace方法不会简化您要完成的任务吗
谢谢,无论如何,主要问题是:第一次我替换内容。值与第一次匹配,然后我替换内容。值与第二次匹配,第一次替换不匹配save@revolutionkpi:代码的一个奇怪之处是,您正在读取
content.Value
并将其分配给节点.content
。您确定在分配给节点.Content
时,更改在内容.Value
中可见吗?我认为将文档中的内容读入一个普通字符串,在字符串上进行所需的替换(s=s.Replace(…);
),然后在最后将最终结果重新分配回文档(而不是在循环内部)会更有意义。但是你真的应该使用HTML敏捷包来做这类事情。这会让你的生活更轻松。@kpi:顺便说一句,我的答案实际上是对你的“主要”问题的回答。如果你认为我的回答没有回答你的问题,那么我想你可能误解了我的回答。您的代码中也可能不是一个错误,而是多个错误。然后我建议你一个接一个地修复它们,不要因为修复一个错误并不能立即解决所有问题而气馁。有时,您需要采取许多小步骤才能到达最终目的地。谢谢,但当我用第二个匹配项替换字符串时,第一个匹配项不保存,因此结果只有最后一个替换项内容的类型是什么?在哪里申报?节点的类型是什么?在哪里申报?
foreach (Match match in matches)
{ // added curly brace here
result = match.Value;
if (result != null)
{
var firstOrDefault = node.ListImages.FirstOrDefault();
if (firstOrDefault != null)
{
var htmlWithImages = content.Value.Replace(result,
string.Format("img src='{0}' class='newsimage' width='300'",
firstOrDefault.ImageUrlId));
node.Content = htmlWithImages;
}
}
} // added curly brace here
foreach (Match match in matches)
{
result = match.Value;
if (result != null)
{
var firstOrDefault = node.ListImages.FirstOrDefault();
if (firstOrDefault != null)
{
var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId));
node.Content = htmlWithImages;
}
}
}