The regular expression is a very flexible language, matching the same string may be different developers will get different results, in the ordinary time is also used when the relevant information, not when it is left behind the brain, although in most cases can be processed quickly, But to deal with some complicated situation is still not high efficiency, through a project previously done by the former has involved the opportunity of regular expression, the relevant information read over and combined with their own experience, organized a few use C # for regular expression programming articles, One to deepen their impressions and understanding, and secondly for the readers to learn from the blog.
in the . NET support for regular expressions is provided, and related classes are provided, respectively: Regex , Match , Group , Capture , regexoptions , matchcollection , groupcollection , capturecollection . The associations between them are as follows:
They are described as follows:
Regex : A regular expression class that represents an immutable regular expression.
Match: Represents theRegexA matching result of an instance of a class, which can beRegexof theMatch ()instance method returns aMatchthe instance.
MatchCollection: Represents theRegexall matching results of an instance of a class, which can beRegexof theMatches ()instance method returns aMatchCollectionthe instance.
Group: Represents the result of a single capturing group. Because one match may contain0A,1One or more groupings, soMatchis the result of a collection of capturing groups that is returned in an instance ofgroupcollection
groupcollection match groups groupcollection Span style= "font-family: Song body; Mso-ascii-font-family: ' Times New Roman '; Mso-hansi-font-family: ' Times New Roman '; > example.
Capture: Represents a substring in a single capture. WithGroup, because a capture may contain0A,1one or more substrings, soGroupreturns the result of the collection of substrings in the instance of thecapturecollection.
capturecollection: By default, the collection of all substrings that are matched by the capturing group in order from the inside out, from left to right, can beGrouporMatchof theCapturesInstance property returnscapturecollectionthe instance. Note that you can useRegexoptions.righttoleftto change the order of the matches.
RegexOptions: Provides an enumeration value for setting the regular expression options. like the above mentionedRightToLeftis one of its enumeration values, in addition toNone,IgnoreCase,Multiline,explicitcapture,Compiled,Singleline,Ignorepatternwhitespace,RightToLeft,ECMAScriptandcultureinvariant. RegexOptionsenumeration values can be added, for example, we want to match a case-insensitive string "ABC"and also want to improve the execution speed, then you can write the following code:
RegexOptions options=regexoptions.ignorecase|regexoptions.compiled;
Regex regex=new Regex ("ABC", options);
regex match group capture
from what can be seenRegexclass provides a number of static methods, and many methods provide multiple overloads (in the figure, the method that has multiple parameter overloads is "..."In addition, we will also find thatCapture,GroupandMatchThere is an inheritance relationship between the two, and I find that there are a lot of the same fields between them when I first started using them, which makes me so confused that I hope you will not be as confused as I was when you see this figure.
when usingC #a regular expression in the text process before you take a moment to understand. NETthere is a need for the classes in the regular expressions and their relationships, and this is a warm-up and warm-up exercise before you start learning regular expressions. Although inC #There are not many classes about regular expressions, but for beginners it is easy to confuse, so that there is no way to know which classes to use which methods or properties of the situation, this is to do a preliminary introduction bar. The next article will first tellRegexclass, usingRegexcan be used to replace, split, and manipulate strings.
C # Regular expression programming (i): Classes in C # about regular