How to work with multiline string using Regex() in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication_C_Sharp
{
    static class Program
    {
        static void Main(string[] args)
        {
            const string s = @"string line 1
string line 2
                               string line 3
string line 4";

            MatchCollection mc = Regex.Matches(s, "^(.+)$", RegexOptions.Multiline);
            foreach (Match match in mc)
            {
                foreach (Capture capture in match.Captures)
                    Console.WriteLine(capture.Value);
            }
        }
    }
}


/*
run:
  
string line 1
string line 2
                               string line 3
string line 4

*/

 



answered Feb 8, 2017 by avibootz
...