C # cainiao Regular Expression 1,
LZ cainiao, who only takes notes and takes notes, adds an impression.
LZ believes that there is no need to be too entangled in the principle, the model, the silk can be used on the right, the rest of the things will naturally explore more.
Chinese: Regular ExPression; English: Regular ExPression, also known as pattern, is used to check whether a string meets a specific rule or capture substrings that meet a specific rule from a string.
Character matching
The simplest regular expression is composed of "common characters" and "wildcard. For example, "Room \ d" is such a regular expression.
Here, "Room" is a common character, while "\ d" is a wildcard, indicating that there is a number at this position. The expression occupies a total of seven positions. The first position is the letter "R", and the second and third positions are both "o ", the fourth position is m, and the fifth to seventh position is three digits. Therefore, the regular expression "Room \ d" represents the type of string that begins with "Room" and ends with three numbers. For example, the string "Room010" and "Room111" Both match "Room \ d.
These special characters are called metacharacters in regular expressions. Because the symbol ". "special characters are used in regular expressions, so you need to express them." "itself, you need to use its escape character" \. ", also express the symbol" \ "itself, you need to use its transfer character" \ ".
. NET provides a batch of classes related to regular expressions, all of which are in the using System. Text. RegularExpressions namespace. Now let's look at the Regex class.
Partial methods of the Regex class
Through Regex. the Matches () method can be used to extract all substrings that match the regular expression from a given string. These substrings are saved in a MatchCollection collection, each substring is considered as an object of the Match Class. Now, assume that an electronic file contains the Kitty room number (in the format of RoomXXX in front). The file is very long and it takes a lot of time and effort to manually check the room number. How can we find the room number through a computer? It's time to sacrifice the regular brother!
The time for the small example is up:
// Namespace using System. text. regularExpressions; string text = "kitty lives in room415, tonny lives in room332"; Regex expression = new Regex (@ "room \ d"); MatchCollection mathes = expression. matches (text); // The matched result is a set of foreach (Match match in mathes) Console. writeLine (match); Match mattings = expression. match (text); // The matching result is a single Console. writeLine (mateline); Console. writeLine (match1.NextMatch (); // match bool match2 = expression. isMatch (text); // check whether the Console is matched. writeLine (match2 );
@ Prefix and Transfer Character
We have learned how to control transfer characters in text format, such as "\ n" "\" "\ t" "\". Now we have learned the transfer characters of regular expressions, for example "\. "" \ w "" \ d "" \ s "" \ "and so on, they are different in regular expressions.
Regex exoression = new Regex ("\ d ");
In this case, an error occurs, because the Backslash "\" is a special character. To represent the backslash itself, you must use its escape character "\". therefore, you need to write the following form:
Regex expression = new Regex ("\ d ");
However, this form reduces readability, So we usually use the @ prefix method.
Regex expression = new Regex (@ "\ d ");
In this case, the transfer operator of the control text format is ignored, but the transfer operator of the regular expression is not ignored.
After the @ prefix is added, if the double quotation mark itself needs to be referenced in the string, it can be represented by consecutive double quotation marks.
Regex expression = new Regex (@ "Say" "Hello """)
@ Optional character set
In addition to wildcards, we can also detach the characters that can appear in a certain position from square brackets [] to form an optional character set, such:
// Optional characters: string text = "Vitor-1970 Verne-1982 Regan-1998 Robin-2008 "; regex regex = new Regex (@ "[VR] [a-z] +-19 [89] [0-9]"); // [VR] [a-z] the first character V or R, the second character a to z, and the plus sign + indicates that the latter is a-z foreach (Match match in regex. matches (text) Console. writeLine (match); reverse character: string text2 = "dog bod fog hog log"; Regex regex2 = new Regex (@ "[^ bd] og "); // The first character is not B or d foreach (var match in regex2.Matches (text2 ))
{
Console. WriteLine (match );
} Console. WriteLine (match );
Or match character
String text3 = "there is a bee in the tree"; Regex regex3 = new Regex (@ "(tr | B) ee "); // tr or B + ee foreach (var match in regex3.Matches (text3) Console. writeLine (match );
Tips: given that regular expressions are hard to write, we can use Baidu to match registration information. If you are not reliable, you can use the "Regex Tester" tool for testing.