Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Regex.Replace() to replace a list of words fails
    primarykey
    data
    text
    <p>I am trying to replace words in a long string with some HTML-tags. I seem to miss something when trying to do this for multiple words from a List, it does however work nicely for single Words.</p> <p>Here is what i tried: For testing purposes i reduced and altered the string to this:</p> <pre><code>string strTest = "Ein toller Testtext mit Folge und Folgen sowie Folgen&lt;p&gt; und &lt;p&gt;Folge und Cauchyfolgen und Cauchy-Folgen und Folgeerscheinungen und Folgendem"; </code></pre> <p>The following code was used as a text to replace "Folge" with "WOW":</p> <pre><code>strResult = Regex.Replace(strTest, "\\b" + "Folge" + "\\b", "WOW"); </code></pre> <p>This resulted in: <code>"Ein toller Testtext mit WOW und Folgen sowie Folgen&lt;p&gt; und &lt;p&gt;WOW und Cauchyfolgen und Cauchy-Folgen und Folgeerscheinungen und Folgendem"</code>, which is the desired result for a single word.</p> <p>The following code resulted in the same, but is using my list of Objects, where "strWort" is the word i want to replace. Note the Value of listTooltips[39].strWort is "Folge".</p> <pre><code>strResult = Regex.Replace(strTest, "\\b" + listTooltips[39].strWort + "\\b", "WOW"); </code></pre> <p>In the next step i tried to do this for the whole list. So i did the following:</p> <pre><code>for (int i = 0; i &lt; listTooltips.Count(); i++) { strResult = Regex.Replace(strTest, "\\b" + listTooltips[i].strWort + "\\b", "WOW"); } </code></pre> <p>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. So i am quite sure the code for the list is not wrong, at least not completly.</p> <p>Additional things i tried:</p> <ul> <li>putting the value of each word into a seperate string and using that for the Regex.Replace. Failed.</li> <li>using a MessageBox.Show() to display the value of listTooltips[i].strWort to make sure, everything outputting as expected.</li> <li>using Regex.Match to check if the above code actually finds anything and yes, it does find everything. It just does not replace ist for some reason.</li> <li>not using Regex and going the string.replace-method instead. This is working with the above loop, but is causing differend unwanted results due to missing a filter / matchmode. i.e. "Folgen" is getting replaced to "WOWn" ect.</li> </ul> <p>Really wondering WHY this is behaing as it does mainly, to better understand it. But id love a solution as well of corse :-)</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload