Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I try your codes, neither does any replacement, because there is no semicolon after the value that you are trying to replace.</p> <p>If you remove the semicolon from the regular expression, both works:</p> <pre><code>html = Regex.Replace(html, @"(['""#(=])" + "oldvalue" + @"(['""#)]|&amp;)", "$1" + "asdf" + "$2", RegexOptions.IgnoreCase); </code></pre> <p>does the same as:</p> <pre><code>var regex = new Regex(@"(['""#(=])" + "oldvalue" + @"(['""#)]|&amp;)", RegexOptions.IgnoreCase); foreach (Match match in regex.Matches(html)) { html = html.Replace(match.Value, match.Groups[1] + "asdf" + match.Groups[2]); } </code></pre> <h3>Edit:</h3> <p>When I try the updated code from the question, it works fine:</p> <pre><code>string html; KeyValuePair&lt;string, string&gt; elementReference = new KeyValuePair&lt;string, string&gt;("oldvalue", "newvalue"); html = "&lt;td&gt;[element]elementreference='oldvalue';[/element]&lt;/td&gt;"; html = Regex.Replace(html, @"(['""#(=])" + elementReference.Key + @"(['""#)];|&amp;)", "$1" + elementReference.Value + "$2", RegexOptions.IgnoreCase); Console.WriteLine(html); html = "&lt;td&gt;[element]elementreference='oldvalue';[/element]&lt;/td&gt;"; var regex = new Regex(@"(['""#(=])" + elementReference.Key + @"(['""#)];|&amp;)", RegexOptions.IgnoreCase); foreach (Match match in regex.Matches(html)) { html = html.Replace(match.Value, match.Groups[1] + elementReference.Value + match.Groups[2]); } Console.WriteLine(html); </code></pre> <p>Output:</p> <pre><code>&lt;td&gt;[element]elementreference='newvalue';[/element]&lt;/td&gt; &lt;td&gt;[element]elementreference='newvalue';[/element]&lt;/td&gt; </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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