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) āļĄāļĩāļāļ§āļēāļĄāļŦāļĄāļēāļĒāļāļąāļāļāļĩāđ
| Pattern | Description |
| (\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




















