C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Example: We specify one or more characters between the start and the end of each line. The plus means "one or more."
Capture: The pattern matches each line in a capture. We use 2 foreach-loops to enumerate the results and print them to the screen.
ForeachInfo: We see how the RegexOptions.Multiline argument was passed as the third parameter to the Regex.Matches static method.
C# program that uses RegexOptions.Multiline
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