Home  >  Article  >  Backend Development  >  How to get all parts of a string that match a regular expression using the Regex.MatchCollection function in C#

How to get all parts of a string that match a regular expression using the Regex.MatchCollection function in C#

王林
王林Original
2023-11-18 13:29:191163browse

How to get all parts of a string that match a regular expression using the Regex.MatchCollection function in C#

How to use the Regex.MatchCollection function in C# to get all parts of a string that match regular expressions, you need specific code examples

Regular expressions are a powerful Pattern matching tool, in C#, you can use the Regex.MatchCollection function to get all parts of the string that match the regular expression. This article explains how to use this function and provides specific code examples.

First, we need to introduce the System.Text.RegularExpressions namespace into the code, which contains regular expression-related classes and methods. This namespace can be introduced with the following code:

using System.Text.RegularExpressions;

We can then use the Regex.MatchCollection function to get all parts of the string that match the regular expression. This function receives two parameters: the string to be matched and the regular expression. Returns a MatchCollection object that contains all matching results.

The following is a simple sample code that demonstrates how to use the Regex.MatchCollection function to get all parts of a string that match the regular expression:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        string input = "Hello, my name is John. My email is john@example.com. Please contact me at john@example.com.";

        // 定义正则表达式
        string pattern = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}";

        // 执行匹配
        MatchCollection matches = Regex.Matches(input, pattern);

        // 遍历所有匹配结果
        foreach (Match match in matches)
        {
            Console.WriteLine(match.Value);
        }

        Console.ReadLine();
    }
}

In the above code, we define Get a string containing email addresses and use a regular expression to match the email addresses in it. This regular expression can match strings that match the mailbox format. Then, we use the Regex.Matches function to match the input string and save all matching results in a MatchCollection object. Finally, we loop through the object and print out all matching email addresses.

Execute the above code and the output result is as follows:

john@example.com
john@example.com

You can see that the code outputs all matching email addresses in the string.

Summary: This article introduces how to use the Regex.MatchCollection function in C# to obtain all parts of a string that match regular expressions. By introducing the System.Text.RegularExpressions namespace and using the Regex.Matches function, we can easily perform regular expression matching and obtain all matching results. I hope this article can be helpful to you when using C# for string matching and regular expression processing!

The above is the detailed content of How to get all parts of a string that match a regular expression using the Regex.MatchCollection function in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn