For a while, regular expression learning was very popular. At that time, I could see several regular expression posts in one day at csdn, during that time, I learned some basic knowledge through the C # string and regular expression reference manual published by the Forum and wrox press, and earned about 1000 points in csdn, today, when I went to the "C # string and regular expression Reference Manual", I was missing it.
(1) "@" symbol
Symbol below two ows table research room hot, when the morning at "@" although not the C # Regular Expression of the "member", but it often with C # Regular Expression out of double inbound. "@" Indicates that the string following it is a "verbatim string", which is not very understandable. For example, the following two statements are equivalent:
String x = "d :\\ my Huang \ My doc ";
String y = @ "D: \ My Huang \ My doc ";
In fact, C # will report an error if it is declared as follows, because "\" is used in C # To implement escape, such as "\ n" line feed:
String x = "D: \ My Huang \ My doc ";
(2) Basic syntax characters.
\ D 0-9 digits
The complete set of \ D (take all the characters as the complete set, the same below), that is, all non-numeric characters
\ W characters, which are uppercase/lowercase letters, 0-9 digits, and underscores
Set of \ W
\ S blank characters, including line breaks \ n, carriage returns \ r, tabs \ t, vertical tabs \ v, line breaks \ f
Set of \ s
. Any character except linefeed \ n
[…] Match All characters listed in []
[^…] Match characters not listed in []
The following provides some simple examples:CopyCodeThe Code is as follows: string I = "\ n ";
String M = "3 ";
RegEx r = new RegEx (@ "\ D ");
// Same as 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 numbers
// R. ismatch (I) Result: false
// R. ismatch (m) Result: True
3) Positioning characters
"Positioning character" represents a virtual character, which represents a location, you can also intuitively think that "positioning character" represents the tiny gap between a character and character.
^ Indicates that the character after it must be at the beginning of the string
$ Indicates that the character before it must be at the end of the string
\ B matches the boundary of a word
\ B matches a non-word boundary
In addition, the character before \ A must be at the beginning of the character, and the character before \ Z must be at the end of the string, the character before \ Z must be at the end of the string or before the line break
The following provides some simple examples:Copy codeThe Code is 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 rows
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 multi-line string, after the multiline option is set, ^ and $ match multiple times.
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