Давайте попробуем удалить все пробелы (\s
) , но \n
и \r
, т.е. [\s-[\r\n]]+
шаблон
Код:
string resource =
"Jim Smith\n\t123 Main St.\n\t\tWherever, ST 99999\n\nFirst line of information.\n\nSecond line of information.";
string fixedResource = Regex.Replace(resource, @"^[\s-[\r\n]]+", "", RegexOptions.Multiline);
Console.Write(fixedResource);
Результат:
Jim Smith
123 Main St.
Wherever, ST 99999
First line of information.
Second line of information.
Редактировать: Если вы хотите обработать коллекцию (скажем, List<string>
), разумно определить Regex
вне цикла (Linq) и т. д. по соображениям эффективности (см. комментарий Panagiotis Kanavos):
List<string> resources = new List<string>() {
"Jim Smith\n\t123 Main St.\n\t\tWherever, ST 99999\n\nFirst line of information.\n\nSecond line of information.",
};
Regex regex = new Regex(@"^[\s-[\r\n]]+", RegexOptions.Multiline);
List<string> fixedResources = resources
.Select(resource => regex.Replace(resource, ""))
.ToList();