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 ...