Edward Tanguay
Edward Tanguay

Reputation: 193302

How to replace leading whitespace but retain multiple lines?

I'm trying to write a regex that removes all leading whitespace like this:

enter image description here

The following code does this, but also greedily removes multiple lines, like this:

enter image description here

How can I change the regex so that it removes preceding whitespace from each line, but leaves the multiple lines intact?

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace test_regex_double_line
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> resources = new List<string>();

            resources.Add("Jim Smith\n\t123 Main St.\n\t\tWherever, ST 99999\n\nFirst line of information.\n\nSecond line of information.");

            foreach (var resource in resources)
            {
                var fixedResource = Regex.Replace(resource, @"^\s+", m => "", RegexOptions.Multiline);
                Console.WriteLine($"{resource}\n--------------\n{fixedResource}\n===========================");
            }
        }
    }
}

Upvotes: 2

Views: 310

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Let's try removing all whitespaces (\s) but \n and \r ones, i.e. [\s-[\r\n]]+ pattern

Code:

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);

Outcome:

Jim Smith
123 Main St.
Wherever, ST 99999

First line of information.

Second line of information.

Edit: If you want to process a collection (say, List<string>) it's reasonable to define Regex outside the loop (Linq) etc. for performance reasons (see Panagiotis Kanavos comment):

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();

Upvotes: 2

Related Questions