title: C# RegEx Replacement
body: <p>I'm adding a profanity filter and what I would like to do is replace the word (or partial word) that's replaced with a string of equal length.  The equal length part is where I'm having difficulty.</p>
  
  <p>So if the word being replaced is 3 characters long than I want the text it's replaced with to be 3 characters long.  I'm not sure how I can substring my replacement string to match the length of the word being replaced.</p>
  
  <p>Here's my test method:</p>
  
  <pre><code>public static string ProfanityFilter(this string text)
  {
      string pattern = @"\bword\b|\bword2\b|\banother*";
      Regex regex = new Regex(pattern);
      string replacement = "*%$@^!#@!@$^()!";
      return regex.Replace(text, replacement);
  }
  </code></pre>
  
  <p>So, if the word "another" is replaced, it would be replaced with "*%$@^!#".</p>
  
  <p>If "word" is replaced it would be replaced with "*%$@^"</p>
  
  <p>If "wording" is replaced it would be replaced with "*%$@^ing"</p>
  
  <p>Update:</p>
  
  <p>I ended up finding the solution...</p>
  
  <p>I created a new method:</p>
  
  <pre><code> public static string Censored(Match match)
          {
              string replacement = "*%$@^!#@!@$^()!";
              return replacement.Substring(0, match.Captures[0].Length);
          }
  </code></pre>
  
  <p>Then changed </p>
  
  <pre><code>return regex.Replace(text, replacement);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>return regex.Replace(text, Censored);
  </code></pre>