Skip to content

Instantly share code, notes, and snippets.

@kipusoep
Created November 19, 2018 08:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kipusoep/ec9c25300bba81bab10144246391af5e to your computer and use it in GitHub Desktop.
Save kipusoep/ec9c25300bba81bab10144246391af5e to your computer and use it in GitHub Desktop.
/// <summary>
/// Replaces the <see cref="regexEscapedSearch" /> occurrences with <see cref="oddReplace" /> and <see cref="evenReplace"/>
/// </summary>
/// <param name="input">The input string</param>
/// <param name="regexEscapedSearch">The string to search for (escaped for usage in regex)</param>
/// <param name="oddReplace">To replace odd occurrences with</param>
/// <param name="evenReplace">To replace even occurrences with</param>
/// <returns></returns>
public static string ReplaceOddEvenOccurrences(this string input, string regexEscapedSearch, string oddReplace, string evenReplace)
{
var i = 0;
return new Regex(regexEscapedSearch).Replace(input, m => i++ % 2 == 0 ? oddReplace : evenReplace);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment