LZ rookie, only to organize notes, incidentally recorded, said to increase the impression.
LZ think, no need too tangled principle, model, cock silk can be used on the right, the rest of the matter with more natural will go to explore.
Chinese: Regular expression, English: Regular expression, also known as pattern, is used to verify whether a string satisfies a particular rule, or to capture a substring that satisfies a particular rule from a string.
character matching
The simplest regular expression consists of "normal character" and "wildcard". For example, "room\d\d\d" is such a regular expression.
where "Room" is a normal character, and "\d" is a wildcard , indicating that there is a number at that location. The expression occupies a total of 7 positions, the first position is the letter "R", and the second and third position is the position "O", the fourth position is "M", and the fifth to the seventh position is three digits. So the regular expression "room\d\d\d" represents the type of string that begins with a "class" and ends with three digits. For example, the string "Room010", "Room111" are matched with "room\d\d\d".
These special characters are called regular in the expression. Because the symbol ". "There are special characters that are useful in regular expressions, so you want to express them." "\" itself, which needs to use its transfer character "\", also expresses the symbol "\" itself, which needs to use its transfer character "\ \".
. NET provides a batch of classes related to regular expressions, all in the using System.Text.RegularExpressions namespace, and now we look at the Regex class.
Partial methods of the Regex class
With the Regex.Matches () method, all substrings matching the regular expression can be decomposed from the given string, which is stored in a matchcollection-type collection, each of which is considered to be the object of the match class. Now suppose that an electronic file contains Kitty's room number (in the form of a roomxxx), the file is long, manual access is laborious, so how to help us find the room number by computer? It's time to sacrifice the regular brother!
The small example time arrived:
namespace using System.Text.RegularExpressions; stringText ="kitty lives in Room415,tonny and lives in room332 ."; Regex expression=NewRegex (@"room\d\d\d"); MatchCollection mathes= expression. Matches (text);//The result of the match is a collection foreach(Match matchinchmathes) Console.WriteLine (match); Match Match1= expression. Match (text);//The matching result is a singleConsole.WriteLine (MATCH1); Console.WriteLine (Match1. NextMatch ());//Next Match BOOLmatch2= expression. IsMatch (text);//whether there is a matchConsole.WriteLine (MATCH2);
@ prefix and transfer character
We have previously learned to control the text format of the transfer character, such as "\ n" "\" "" \ T "" \ \ "And so on, and now learn the regular expression of the transfer character, such as" \. "" \w "\d" "\s" "\ \" etc., they are different in the regular.
Regex exoression=new Regex ("\d");
This will cause an error, because the backslash "\" itself is a special character, to represent the backslash itself, you need to use its transfer "\ \", so you need to write the following form:
Regex expression =new regex ("\\d");
But this form will reduce readability, so we usually use the @ prefix method.
Regex expression =new Regex (@ "\d");
The transfer character that controls the format of the text is ignored, but the transfer character of the regular expression is not ignored.
After adding the prefix @, if the string needs to refer to the double quotation mark itself, it can be expressed in consecutive double quotation marks.
Regex expression=new Regex (@ "Say ""Hello" "")
@ Optional Character Set
In addition to wildcards, we can also put the allowed characters in a position to unload square brackets [], to form an optional character set, such as:
//Optional characters: stringText ="Vitor-1970 Verne-1982 Regan-1998 Robin-2008"; Regex regex=NewRegex (@"[vr][a-z]+-19[89][0-9]");//[Vr][a-z] The first character V or R, the second character A to z,+ indicates a A-Z followed by a foreach(Match matchinchregex. Matches (text)) Console.WriteLine (match); Reverse character:stringText2 ="dog bod fog hog log"; Regex Regex2=NewRegex (@"[^bd]og");//first word printable B or D foreach(varMatchinchRegex2. Matches (Text2))
{
Console.WriteLine (match);
} Console.WriteLine (Match);
or a matching character
string " There is a bee in the tree " ; New Regex (@ "(tr|b) EE"//tr or b+ee foreach ( var in regex3. Matches (TEXT3)) Console.WriteLine (match);
Tips: In view of the difficult to write regular expressions, commonly used we can Baidu, into matching registration information, and so on, if you write a non-reliable, you can use the "Regex Tester" tool to test.
C # rookie Regular expression one