Regular Expressions āļŦāļĢāļ·āļ­āđ€āļĢāļĩāļĒāļāļĒāđˆāļ­āđ† āļ§āđˆāļē “regex” āđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļāļģāļŦāļ™āļ”āļĢāļđāļ›āđāļšāļšāļŦāļĢāļ·āļ­āļāļĨāļļāđˆāļĄāļ„āļģ āđ€āļžāļ·āđˆāļ­āđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļ„āđ‰āļ™āļŦāļēāđƒāļ™āļ‚āđ‰āļ­āļ„āļ§āļēāļĄāļ•āđˆāļēāļ‡āđ† āļ•āļēāļĄāļ—āļĩāđˆāđ€āļĢāļēāļ•āđ‰āļ­āļ‡āļāļēāļĢ

regex āļŠāļēāļĄāļēāļĢāļ–āđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļ„āđ‰āļ™āļŦāļēāđƒāļ™āļĢāļđāļ›āđāļšāļšāļ‚āļ­āļ‡āļ‚āđ‰āļ­āļ„āļ§āļēāļĄāļ˜āļĢāļĢāļĄāļ”āļē āļ•āļąāļ§āđ€āļĨāļ‚ āļŦāļĢāļ·āļ­āļ­āļąāļāļĐāļĢāļ°āļžāļīāđ€āļĻāļĐ āđ‚āļ”āļĒāđ€āļĢāļēāļŠāļēāļĄāļēāļĢāļ–āļāļģāļŦāļ™āļ”āđ„āļ”āđ‰āļ§āđˆāļēāļˆāļ°āđƒāļŦāđ‰ match āđāļ„āđˆ 1 āļ­āļąāļ™ āļŦāļĢāļ·āļ­ match āđ„āļ”āđ‰āļŦāļĨāļēāļĒāđ† āļ­āļąāļ™ āļŦāļĢāļ·āļ­āđ„āļĄāđˆ match āđ€āļĨāļĒāļāđ‡āđ„āļ”āđ‰ āļāļēāļĢāļ™āļģāđ„āļ›āļ›āļĢāļ°āļĒāļļāļāļ•āđŒāđƒāļŠāđ‰āļ‡āļēāļ™ āđ€āļŠāđˆāļ™ āļāļēāļĢāļ•āļĢāļ§āļˆāļŠāļ­āļš input āļ§āđˆāļēāđ€āļ›āđ‡āļ™āļ‚āđ‰āļ­āļĄāļđāļĨāļ—āļĩāđˆāđ€āļĢāļēāļ•āđ‰āļ­āļ‡āļāļēāļĢāļŦāļĢāļ·āļ­āđ„āļĄāđˆ

regex āļĄāļĩāđƒāļŦāđ‰āđƒāļŠāđ‰āļ‡āļēāļ™āđƒāļ™āđ€āļāļ·āļ­āļšāļ—āļļāļāļ āļēāļĐāļē āđāļ•āđˆāđƒāļ™āļšāļ—āļ„āļ§āļēāļĄāļ™āļĩāđ‰āļ‚āļ­āđ€āļŠāļ™āļ­āļāļēāļĢāđƒāļŠāđ‰āļ‡āļēāļ™ regex match āđƒāļ™āļĢāļđāļ›āđāļšāļšāļ‚āļ­āļ‡āļ āļēāļĐāļē C# āļ„āļĢāļąāļš

Regex.Match Method
Namespace: System.Text.RegularExpressions
Assembly: System.Text.RegularExpressions.dll

Regex.Match āđ€āļ›āđ‡āļ™ method overloads āļĄāļĩāđƒāļŦāđ‰āđ€āļĨāļ·āļ­āļāđƒāļŠāđ‰āļ‡āļēāļ™āļŦāļĨāļēāļĒāļĢāļđāļ›āđāļšāļš āļ”āļąāļ‡āļ•āļēāļĢāļēāļ‡āļ”āđ‰āļēāļ™āļĨāđˆāļēāļ‡
āđƒāļ™āļšāļ—āļ„āļ§āļēāļĄāļ™āļĩāđ‰āļˆāļ°āđ€āļ›āđ‡āļ™āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāđƒāļŠāđ‰āļ‡āļēāļ™ Regex.Match(String) āļ™āļ°āļ„āļĢāļąāļš

Overloads
Match(String, String, RegexOptions, TimeSpan)Searches the input string for the first occurrence of the specified regular expression, using the specified matching options and time-out interval.
Match(String, Int32, Int32)Searches the input string for the first occurrence of a regular expression, beginning at the specified starting position and searching only the specified number of characters.
Match(String, String, RegexOptions)Searches the input string for the first occurrence of the specified regular expression, using the specified matching options.
Match(String, Int32)Searches the input string for the first occurrence of a regular expression, beginning at the specified starting position in the string.
Match(String)Searches the specified input string for the first occurrence of the regular expression specified in the Regex constructor.
Match(String, String)Searches the specified input string for the first occurrence of the specified regular expression.

Match(String)
āđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļ„āđ‰āļ™āļŦāļēāļ„āļģāļŦāļĢāļ·āļ­āļ›āļĢāļ°āđ‚āļĒāļ„āļ—āļĩāđˆāļ•āđ‰āļ­āļ‡āļāļēāļĢāļˆāļēāļ regex āđ‚āļ”āļĒ method āļ™āļĩāđ‰āļˆāļ° return match āļ­āļąāļ™āđāļĢāļāļ—āļĩāđˆāļžāļšāđƒāļ™ input string

using System;
using System.Text.RegularExpressions;

namespace Code4Sec.RegexMatchSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "Hello! One Code4Sec Two Code4Sec Three Code4Sec";
            string pattern = @"(\w+)\s+(Code4Sec)";

            // Instantiate the regular expression object.
            Regex r = new Regex(pattern, RegexOptions.IgnoreCase);

            // Match the regular expression pattern against a text string.
            Match m = r.Match(text);
            int matchCount = 0;
            while (m.Success)
            {
                Console.WriteLine("Match" + (++matchCount));
                for (int i = 1; i <= 2; i++)
                {
                    Group g = m.Groups[i];
                    Console.WriteLine("Group" + i + "='" + g + "'");
                    CaptureCollection cc = g.Captures;
                    for (int j = 0; j < cc.Count; j++)
                    {
                        Capture c = cc[j];
                        System.Console.WriteLine("Capture" + j + "='" + c + "', Position=" + c.Index);
                    }
                }
                m = m.NextMatch();
            }
        }
    }
}

āļˆāļēāļāļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ”āđ‰āļēāļ™āļšāļ™ āđ„āļ”āđ‰āļĢāļ°āļšāļļ regular expression pattern (\w+)\s+(Code4Sec) āļĄāļĩāļ„āļ§āļēāļĄāļŦāļĄāļēāļĒāļ”āļąāļ‡āļ™āļĩāđ‰

PatternDescription
(\w+)āļ‚āļķāđ‰āļ™āļ•āđ‰āļ™āļ”āđ‰āļ§āļĒāļ„āļģāļ­āļ°āđ„āļĢāļāđ‡āđ„āļ”āđ‰
\s+āļĄāļĩāļŠāđˆāļ­āļ‡āļ§āđˆāļēāļ‡āļĢāļ°āļŦāļ§āđˆāļēāļ‡āļ„āļģ
(Code4Sec)āļ•āđ‰āļ­āļ‡āđ€āļ›āđ‡āļ™āļ„āļģāļ§āđˆāļē “Code4Sec” āđƒāļ™āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āđ„āļ”āđ‰āļĢāļ°āļšāļļ RegexOptions.IgnoreCase āđ€āļžāļ·āđˆāļ­āđ€āļ›āđ‡āļ™āļāļēāļĢāļšāļ­āļāļ§āđˆāļēāđ€āļ›āđ‡āļ™āļ­āļąāļāļĐāļĢāļžāļīāļĄāļžāđŒāđ€āļĨāđ‡āļāļŦāļĢāļ·āļ­āļžāļīāļĄāļžāđŒāđƒāļŦāļāđˆāļāđ‡āđ„āļ”āđ‰

āđ€āļĄāļ·āđˆāļ­ Run āļ„āļģāļŠāļąāđˆāļ‡āđāļĨāđ‰āļ§āļˆāļ°āđ„āļ”āđ‰āļœāļĨāļĨāļąāļžāļ˜āđŒāļ”āļąāļ‡āļ āļēāļž

āļœāļĨāļĨāļąāļžāļ˜āđŒāļ—āļĩāđˆāđ„āļ”āđ‰āļˆāļēāļ Match method āļˆāļ° return āđ€āļ›āđ‡āļ™ substring āđāļĢāļāļ—āļĩāđˆāļžāļšāđƒāļ™āļ›āļĢāļ°āđ‚āļĒāļ„
āđƒāļ™āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ›āļĢāļ°āđ‚āļĒāļ„āđāļĢāļāļ—āļĩāđˆ match āļ„āļ·āļ­ “One Code4Sec” āđƒāļ™āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļĄāļĩāļāļēāļĢāđƒāļŠāđ‰āļĨāļđāļ› while āđāļĨāļ°āļ„āļģāļŠāļąāđˆāļ‡ m.NextMatch() āđ€āļžāļ·āđˆāļ­āđƒāļŦāđ‰āļŦāļēāļ„āļģāļ—āļĩāđˆ match āđƒāļ™āļĨāļģāļ”āļąāļšāļ–āļąāļ”āđ„āļ›āļ„āļĢāļąāļš

Mirror: https://athipornp.github.io/2021/01/04/RegexMatch.html