First, in C #, to use the regular expression class, add the following statement at the beginning of the source file:
using System.Text.RegularExpressions;
Second, the Regex class commonly used methods
1. Static Match method
Using the static Match method, you can get a continuous substring of the first matching pattern in the source.
the static Match method has 2 overloads, each of which is
Regex.match (string input, string pattern);
regex.match (string input, string pattern, regexoptions options);
parameter representation of the first Overload: input, Mode
The second overloaded parameter is represented by a bitwise OR combination of the input, pattern, RegexOptions enumeration.
The valid values for the RegexOptions enumeration are:
Complied means compiling this pattern
Cultureinvariant says no cultural background is considered
ECMAScript to ECMAScript, this value can only be used in conjunction with ignorecase, Multiline, complied
Explicitcapture indicates that only explicitly named groups are saved
IgnoreCase indicates an input-insensitive case
Ignorepatternwhitespace indicates that the non-escaped whitespace in the pattern is removed and the note marked by the # is enabled
Multiline represents multiline mode, which alters the meaning of metacharacters ^ and $, and they can match the beginning and end of a line
None means no setting, this enumeration item has no meaning
RightToLeft represents right-to-left scanning, matching, at which point the static match method returns a right-to-left first match
Singleline represents a single-line pattern that alters metacharacters. meaning that it can match newline characters
Note: Multiline can be used with Singleline without ECMAScript. Singleline and Multiline are not mutually exclusive, but mutually exclusive with ECMAScript.
2. Static Matches method
The overloaded form of this method is the same as the static Match method, which returns a matchcollection that represents the matching set of patterns in the input.
3. Static IsMatch method
This method returns a bool, overloaded form with static matches, if the input matches the pattern, returns true, otherwise false is returned.
Can be understood as: IsMatch method, returns whether the collection returned by the matches method is empty.
Introduction to the Regex class for C # regular expressions