C # 's string interception--regex.match

Source: Internet
Author: User
The first two blogs "C # string Intercept--substring" and "C # string interception--split" respectively introduced the Substring function and the Split function, but also achieved the "affiliation name/Teacher Name/course Type/Course name" in the name of the organization , the teacher name, course type, the course name intercepts the function separately. Today again to introduce you how to use regular expressions to achieve this function.


Regex.match Method:
Searches the input string for a substring that matches the regular expression pattern and returns the first occurrence as a single match object.


There are six types of overloaded methods in the Regex.match method, and all the same, the following is mainly to tell us how to use the Regex.match method to achieve the functions we need:


Public match match (string input): searches the specified input string for the first occurrence of the regular expression specified in the Regex constructor; parameter: A string to search for a match; return result: A match object that contains information about the match.


Match object: Represents the result of a single regular expression match. Where the groups property of the match object is: Gets the collection of groups that are matched by the regular expression.

If we're going to use the Regex.match method, first we'll add a using System.Text.RegularExpressions;

static void Main (string[] args)
        {
            string s1 = "Affiliation Name/Teacher Name/course Type/course name";

            Define a regular expression that can intercept what we need, the regex regex
            = new regex (. *)/(. *)/(. *)/(. *), regexoptions.ignorecase);
            
            Displays each substring of a regular expression,
            foreach (var i in regex). Match (S1). Groups)
            {
                Console.WriteLine (i);
            }
        }





The Regex.match method also implements the string interception function we want, but the construction of regular expressions is one of our weaknesses compared to the previous substring function and the split function. But as long as we take a little time to study hard, in fact, the regular expression is not so difficult to understand ...

The string intercept function is now summed up so much, perhaps there are other better ways to understand later ...







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.