title: Replace only substrings that have specified string before and specified string after
body: <p>I want replace substrings in string that have specified substring before and specified substring after.</p>
  
  <p>I have dictionary:</p>
  
  <pre><code>Dictionary&lt;string, string&gt; map = new Dicrionary&lt;string, string&gt;
  {
     {"From","DateFrom"}
  }
  </code></pre>
  
  <p>which represents transformation of names in source string.</p>
  
  <p>Then I have:</p>
  
  <pre><code>var source = "(From|DistanceFrom|Name)";
  foreach (var key in map.Keys)
  {
     var findWholeVarsNamesPattern = $"^({key})$|(\\({key}\\|)|(\\|{key}\\))|(\\|{key}\\|)";
     source = Regex.Replace(source, map[key], findWholeVarsNamesPattern);
  }
  </code></pre>
  
  <p>How can I modify <code>findWholeVarsNamesPattern</code> to replace only <code>"From"</code> substring in source instead of <code>"(From|"</code> but not modify <code>"DistanceFrom"</code>?</p>
  
  <p>So to summarize I expect to replace word <code>"From"</code> that have:</p>
  
  <ul>
  <li>nothing after and before or</li>
  <li>have "(" before and "|" after or</li>
  <li>have "|" before and "|" after or</li>
  <li>have "|" before and ")" after.</li>
  </ul>