Regex Starts With Example

CSharp · Console

/Program.cs

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

List<string> inputs = new()
{
	"Hello, World!",
	"Something something Hello!",
	"  Hello",
	"Hello from Dev Leader!",
};

string pattern = "^Hello";
Regex regex = new Regex(pattern);

foreach (var input in inputs)
{
	Match match = regex.Match(input);
    Console.WriteLine(
        $"'{input}' {(match.Success ? "did" : "did not")} " +
		"match the string starting with the pattern.");
}
;