For a while, regular expression learning was hot and trendy, and at that time, several regular expression posts could be seen in the Csdn day, and some of the basics were learned with the help of the Forum and the "C # string and regular Expression reference manual" published by Wrox Press. At the same time also for me in Csdn probably earned 1000 points, today, to find the "C # string and regular Expression reference manual", has been missing.
(1) "@" symbol
The two ows of the table are hot, when the Morning "@" although not a C # regular expression of the "Member", but it is often with C # regular expression out of pairs. "@" means that the string following it is a "verbatim string", which is not very well understood, for example, where two declarations are equivalent:
String x= "D:\\my huang\\my Doc";
String y = @ "D:\My huang\my Doc";
In fact, C # will complain if you follow the following statement, because "\" is used in C # for escaping, such as "\ n" line wrapping:
string x = "D:\My huang\my Doc";
(2) basic grammatical characters.
Number of \d 0-9
\d \d's complement (so that the word identifier complete, the same below), that is, all non-numeric characters
\w A word character, a number of uppercase and lowercase letters, 0-9, underline
The complement of \w \w
\s White-space characters, including line breaks \ n, carriage returns \ r, tabs \ T, vertical tab \v, page breaks \f
The complement of \s \s
. Any character other than the newline character \ n
[...] Matches all the characters listed in []
[^ ...] Match characters listed in non []
Some simple examples are provided below:
Copy Code code as follows:
string i = "\ n";
String m = "3";
Regex r = new Regex (@ "\d");
Same regex r = new Regex ("\\d");
R.ismatch (i) Result: true
R.ismatch (m) Result: false
string i = "%";
String m = "3";
Regex r = new Regex ("[a-z0-9]");
Match lowercase letters or numeric characters
R.ismatch (i) Result: false
R.ismatch (m) Result: True
3) Locating characters
"Positioning character" represents a virtual character, which represents a position, you can also intuitively think that "positioning character" represents a character and the small gap between the characters.
^ indicates that subsequent characters must be at the beginning of the string
$ indicates that the character before it must be at the end of the string
\b Match the bounds of a word
\b Matches a non-word boundary
In addition, it includes the following: \a the preceding character must be at the beginning of the character, \z the preceding character must be at the end of the string, \z the preceding character must be at the end of the string, or before the line feed
Some simple examples are provided below:
Copy Code code as follows:
string i = "Live for Nothing,die for something";
Regex r1 = new Regex ("^live for Nothing,die for something$");
R1. IsMatch (i) True
Regex r2 = new Regex ("^live for Nothing,die for some$");
R2. IsMatch (i) false
Regex r3 = new Regex ("^live for Nothing,die for some");
R3. IsMatch (i) True
string i = @ "Live for nothing,
Die for something ";/Multiple lines
Regex r1 = new Regex ("^live for Nothing,die for something$");
Console.WriteLine ("R1 Match count:" + R1.) Matches (i). Count);//0
Regex r2 = new Regex ("^live for Nothing,die for something$", regexoptions.multiline);
Console.WriteLine ("R2 Match count:" + R2.) Matches (i). Count);//0
Regex r3 = new Regex ("^live for Nothing,\r\ndie for something$");
Console.WriteLine ("R3 Match count:" + R3.) Matches (i). Count);//1
Regex r4 = new Regex ("^live for nothing,$");
Console.WriteLine ("R4 match count:" + R4.) Matches (i). Count);//0
Regex R5 = new Regex ("^live for nothing,$", regexoptions.multiline);
Console.WriteLine ("R5 Match count:" + R5.) Matches (i). Count);//0
Regex r6 = new Regex ("^live for nothing,\r\n$");
Console.WriteLine ("R6 match count:" + R6.) Matches (i). Count);//0
Regex R7 = new Regex ("^live for nothing,\r\n$", regexoptions.multiline);
Console.WriteLine ("R7 Match count:" + R7.) Matches (i). Count);//0
Regex r8 = new Regex ("^live for nothing,\r$");
Console.WriteLine ("R8 Match count:" + R8.) Matches (i). Count);//0
Regex R9 = new Regex ("^live for nothing,\r$", regexoptions.multiline);
Console.WriteLine ("R9 Match count:" + R9.) Matches (i). Count);//1
Regex R10 = new Regex ("^die for something$");
Console.WriteLine ("R10 Match count:" + R10.) Matches (i). Count);//0
Regex R11 = new Regex ("^die for something$", regexoptions.multiline);
Console.WriteLine ("R11 Match count:" + R11.) Matches (i). Count);//1
Regex R12 = new Regex ("^");
Console.WriteLine ("R12 Match count:" + R12.) Matches (i). Count);//1
Regex R13 = new Regex ("$");
Console.WriteLine ("R13 Match count:" + R13.) Matches (i). Count);//1
Regex R14 = new Regex ("^", regexoptions.multiline);
Console.WriteLine ("R14 Match count:" + R14.) Matches (i). Count);//2
Regex R15 = new Regex ("$", regexoptions.multiline);
Console.WriteLine ("R15 Match count:" + R15.) Matches (i). Count);//2
Regex R16 = new Regex ("^live for Nothing,\r$\n^die for something$", regexoptions.multiline);
Console.WriteLine ("R16 match count:" + R16.) Matches (i). Count);//1
For a multiline string, ^ and $ will appear multiple matches after the multiline option is set.
string i = "Live for Nothing,die for something";
string m = "Live for Nothing,die for some thing";
Regex r1 = new Regex (@ "\bthing\b");
Console.WriteLine ("R1 Match count:" + R1.) Matches (i). Count);//0
Regex r2 = new Regex (@ "thing\b");
Console.WriteLine ("R2 Match count:" + R2.) Matches (i). Count);//2
Regex r3 = new Regex (@ "\bthing\b");
Console.WriteLine ("R3 Match count:" + R3.) Matches (m). Count);//1
Regex r4 = new Regex (@ "\bfor something\b");
Console.WriteLine ("R4 match count:" + R4.) Matches (i). Count);//1
\b is usually used to constrain a complete word
current 1/3 page
1 23 Next read the full text