Ваш вопрос сводится к, как использовать регулярные выражения в C #?
Ответ - класс Regex
. Для выполнения замены вам необходимо Regex.Replace()
. Нет необходимости явно компилировать регулярное выражение, потому что это делается при создании экземпляра Regex
.
Следующий пример из MSDN иллюстрирует, как использовать класс:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "This is text with far too much " +
"whitespace.";
string pattern = "\\s+";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
}
}
// The example displays the following output:
// Original String: This is text with far too much whitespace.
// Replacement String: This is text with far too much whitespace.