简体   繁体   English

使用Regex.Replace()替换单词列表失败

[英]Using Regex.Replace() to replace a list of words fails

I am trying to replace words in a long string with some HTML-tags. 我正在尝试用一些HTML标签替换长字符串中的单词。 I seem to miss something when trying to do this for multiple words from a List, it does however work nicely for single Words. 当尝试对列表中的多个单词执行此操作时,我似乎错过了一些内容,但是对于单个单词确实可以很好地工作。

Here is what i tried: For testing purposes i reduced and altered the string to this: 这是我尝试的方法:为了进行测试,我将字符串减少并更改为:

string strTest = "Ein toller Testtext mit Folge und Folgen sowie Folgen<p> und <p>Folge und Cauchyfolgen und Cauchy-Folgen und Folgeerscheinungen und Folgendem";

The following code was used as a text to replace "Folge" with "WOW": 以下代码用作将“ Folge”替换为“ WOW”的文本:

strResult = Regex.Replace(strTest, "\\b" + "Folge" + "\\b", "WOW");

This resulted in: "Ein toller Testtext mit WOW und Folgen sowie Folgen<p> und <p>WOW und Cauchyfolgen und Cauchy-Folgen und Folgeerscheinungen und Folgendem" , which is the desired result for a single word. 结果是: "Ein toller Testtext mit WOW und Folgen sowie Folgen<p> und <p>WOW und Cauchyfolgen und Cauchy-Folgen und Folgeerscheinungen und Folgendem"是一个单词的理想结果。

The following code resulted in the same, but is using my list of Objects, where "strWort" is the word i want to replace. 以下代码产生了相同的结果,但是使用的是我的对象列表,其中“ strWort”是我要替换的单词。 Note the Value of listTooltips[39].strWort is "Folge". 注意listTooltips [39] .strWort的值为“ Folge”。

strResult = Regex.Replace(strTest, "\\b" + listTooltips[39].strWort + "\\b", "WOW");

In the next step i tried to do this for the whole list. 在下一步中,我尝试对整个列表执行此操作。 So i did the following: 所以我做了以下事情:

for (int i = 0; i < listTooltips.Count(); i++)
{
    strResult = Regex.Replace(strTest, "\\b" + listTooltips[i].strWort + "\\b", "WOW");
}

The result is an unchanged String. 结果是未更改的字符串。 And i am lost as to why. 我迷失了原因。 As noted it does work if i only put one Word to replace, even if i use a specific object from my list. 如前所述,即使我仅使用一个Word来替换它,即使我使用列表中的特定对象也可以。 So i am quite sure the code for the list is not wrong, at least not completly. 因此,我非常确定列表的代码没有错误,至少没有完全错误。

Additional things i tried: 我尝试过的其他事情:

  • putting the value of each word into a seperate string and using that for the Regex.Replace. 将每个单词的值放入一个单独的字符串中,并将其用于Regex.Replace。 Failed. 失败。
  • using a MessageBox.Show() to display the value of listTooltips[i].strWort to make sure, everything outputting as expected. 使用MessageBox.Show()显示listTooltips [i] .strWort的值以确保所有内容均按预期输出。
  • using Regex.Match to check if the above code actually finds anything and yes, it does find everything. 使用Regex.Match来检查以上代码是否确实找到了任何内容,是的,它确实找到了所有内容。 It just does not replace ist for some reason. 它只是由于某种原因不能取代ist。
  • not using Regex and going the string.replace-method instead. 不使用Regex而是使用string.replace-method。 This is working with the above loop, but is causing differend unwanted results due to missing a filter / matchmode. 这与上面的循环一起工作,但是由于缺少过滤器/匹配模式而导致差分结果不良。 ie "Folgen" is getting replaced to "WOWn" ect. 即“ Folgen”将被替换为“ WOWn”等。

Really wondering WHY this is behaing as it does mainly, to better understand it. 真的很想知道为什么这样做,主要是为了更好地理解它。 But id love a solution as well of corse :-) 但是id也喜欢corse的解决方案:-)

Thanks in advance! 提前致谢!

In your for loop, you're always performing a Regex.Replace() on the original text strTest . for循环中,您总是在原始文本strTest上执行Regex.Replace()

You need to call it on the previously-replaced text in strResult , otherwise each replacement (except for the very last one) will be overwritten with the original text. 您需要在strResult先前替换的文本上调用它,否则每次替换(最后一个替换除外)都将被原始文本覆盖。

Try this: 尝试这个:

string strResult = strTest;
for (int i = 0; i < listTooltips.Count(); i++) {
    strResult = Regex.Replace(strResult , "\\b" + listTooltips[i].strWort + "\\b", "WOW");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM