
RegexOptions.Multiline makes handling multiple lines easier. You want to apply a regular expression over an input string that has many lines. Each line in the input string should be tested against the entire regular expression. We apply the RegexOptions.Multiline enumerated constant.
This C# program uses Regex.Matches and RegexOptions.Multiline.

First, this article deals with some specific control characters in the regular expression pattern. When specifying a pattern, you can use the ^ character to specify the start of the input string, and the $ character to specify the very end of it.
In this program, we specify the one or more characters between the start and the end of each line, thereby matching each line in a capture. We use two foreach loops to enumerate the results and print them to the screen.
Program that uses RegexOptions.Multiline [C#]
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// Input string.
const string example = @"This string
has two lines";
// Get a collection of matches with the Multiline option.
MatchCollection matches = Regex.Matches(example, "^(.+)$", RegexOptions.Multiline);
foreach (Match match in matches)
{
// Loop through captures.
foreach (Capture capture in match.Captures)
{
// Display them.
Console.WriteLine("--" + capture.Value);
}
}
}
}
Output
--This string
--has two lines
Notes. You can see how the RegexOptions.Multiline argument was passed as the third parameter to the Regex.Matches static method. This is how you can use an enumerated constant with the Regex type methods. There are other RegexOptions you can consider, but they are not covered here. For information about the Kleene closure, represented by the + symbol, please see the pertinent article.
Star: Regex
In this tutorial, we looked at the RegexOptions.Multiline enumerated constant, which is useful for treating each separate line in an input string as a separate input. This can make dealing with long input strings much easier, particularly those that contain specially formatted values.
Regex Type