The namespace using system. Text. regularexpressions needs to be introduced. (If this parameter is not introduced, system. Text. regularexpressions. RegEx must be written when RegEx is written)
Replace method of Regular Expression
String T = "sdf1234sdf12sd12 ";
T = system. Text. regularexpressions. RegEx. Replace (T, @ "/d + [A-Za-Z] + ","");
@ "/D + [A-Z] + | [A-Z] +" match the numbers and letters; system. text. regularexpressions. regEx. replace (T, @ "/d + [A-Z] + | [A-Z] + ",""); the character T is replaced by a null string that matches a number with a letter.
(1) "@" symbol
Although "@" is not a "member" of the C # regular expression, it often has a pair with the C # regular expression. "@" 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 complement set of/D/d (take all the characters as the complete set, the same below), that is, all non-numeric characters
/W words, uppercase/lowercase letters, 0-9 digits, and underscores
/W completion set
/S blank characters, including linefeed/N, carriage return/R, Tab/t, vertical TAB/V, break/F
/S completion set
Any character except linefeed/n
[…] Match All characters listed in []
[^…] Match characters not listed in []
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
(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 character string, the character before/Z must be at the end of the string or before the line break.
String I = "Live for nothing, die for something ";
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 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
/// B is usually used to constrain a complete word
(4) repeated description characters
"Repeated description characters" is one of the places that reflect C # regular expressions "very powerful:
{N} matches the previous CHARACTER n times
{N,} matches the previous CHARACTER n times or more than N times
{N, m} matches the previous characters n to m
? Match the first character 0 or 1 time
+ Match the previous character once or more
* Match the first character 0 times or equal to 0 times
String y = "(+) 1024 ";
String Z = "1,024 ";
String A = "1 ";
String B = "-1024 ";
String c = "10000 ";
RegEx r = new RegEx (@ "^/+? [1-9],? /D {3} $ ");
Console. writeline ("x match count:" + R. Matches (x). Count); // 1
Console. writeline ("y match count:" + R. Matches (Y). Count); // 1
Console. writeline ("Z match count:" + R. Matches (Z). Count); // 1
Console. writeline ("A match count:" + R. Matches (a). Count); // 0
Console. writeline ("B match count:" + R. Matches (B). Count); // 0
Console. writeline ("C match count:" + R. Matches (c). Count); // 0
// Match the integer between 1000 and 9999.
(5) select one matching
The (|) symbol in the C # regular expression does not seem to have a special title, so it is called "select a match. In fact, like [A-Z] is also an alternative match, except that it can only match a single character, while (|) provides a larger range, (AB | xy) matches AB or XY. Note that "|" and "()" are a whole. The following provides some simple examples:
String x = "0 ";
String y= "0.23 ";
String Z = "100 ";
String A = "100.01 ";
String B = "9.9 ";
String c = "99.9 ";
String d = "99 .";
String E = "00.1 ";
RegEx r = new RegEx (@ "^/+? (100 (. 0 +) *) | ([1-9]? [0-9]) (/./d +) *) $ ");
Console. writeline ("x match count:" + R. Matches (x). Count); // 1
Console. writeline ("y match count:" + R. Matches (Y). Count); // 1
Console. writeline ("Z match count:" + R. Matches (Z). Count); // 1
Console. writeline ("A match count:" + R. Matches (a). Count); // 0
Console. writeline ("B match count:" + R. Matches (B). Count); // 1
Console. writeline ("C match count:" + R. Matches (c). Count); // 1
Console. writeline ("D match count:" + R. Matches (d). Count); // 0
Console. writeline ("e match count:" + R. Matches (e). Count); // 0
// Match the number from 0 to 100. The outer brackets contain two parts: "(100 (. 0 +) *)" and "([1-9]? [0-9]) (/. /d +) * ", the two parts are the" or "relationship, that is, the Regular Expression Engine will first try to match 100. If it fails, then try to match the last expression (representing a number in the range of [0,100 ).