C # regular expression,
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. At present, regular expressions are used less often. Sort out the previous notes and do not forget them.
(1) "@" symbol
Symbol below two ows table research room hot, when the morning at "@" although not a C # Regular Expression of "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 # reports 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 \ 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 \ w
\ S blank characters, including line breaks \ n, carriage returns \ r, tabs \ t, vertical tabs \ v, line breaks \ f
Set of \ S \ s
. Any character except linefeed \ n
[…] Match All characters listed in []
[^…] Match characters not listed in []
The following provides some simple examples:
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:
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
(4) repeated description characters
"Repeated description character" is one of the places that reflect the C # regular expression "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
The following provides some simple examples:
String * = "1024 ";
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 here. 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 +) *)", "([1-9]? [0-9]) (\. \ d +) * ", the relationship between the two parts is" OR ", 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 [0,100 ).
(6) Matching of special characters
The following provides some simple examples:
String x = "\\";
Regex r1 = new Regex ("^ \\\\ $ ");
Console. WriteLine ("r1 match count:" + r1.Matches (x). Count); // 1
Regex r2 = new Regex (@ "^ \ $ ");
Console. WriteLine ("r2 match count:" + r2.Matches (x). Count); // 1
Regex r3 = new Regex ("^ \ $ ");
Console. WriteLine ("r3 match count:" + r3.Matches (x). Count); // 0
// Match "\"
String x = "\"";
Regex r1 = new Regex ("^ \" $ ");
Console. WriteLine ("r1 match count:" + r1.Matches (x). Count); // 1
Regex r2 = new Regex (@ "^" "$ ");
Console. WriteLine ("r2 match count:" + r2.Matches (x). Count); // 1
// Match double quotes
(7) group and non-capture group
The following provides some simple examples:
String x = "Live for nothing, die for something ";
String y = "Live for nothing, die for somebody ";
Regex r = new Regex (@ "^ Live ([a-z] {3}) no ([a-z] {5 }), die \ 1 some \ 2 $ ");
Console. WriteLine ("x match count:" + r. Matches (x). Count); // 1
Console. WriteLine ("y match count:" + r. Matches (y). Count); // 0
// The Regular Expression Engine remembers the matched content in "()" as a "group" and can be referenced by indexing. The "\ 1" in the expression is used to reference the first group in the expression in reverse direction, that is, the content of the first bracket marked in bold, and "\ 2.
String x = "Live for nothing, die for something ";
Regex r = new Regex (@ "^ Live for no ([a-z] {5}), die for some \ 1 $ ");
If (r. IsMatch (x ))
{
Console. WriteLine ("group1 value:" + r. Match (x). Groups [1]. Value); // output: thing
}
// Obtain the content in the group. Note: This is Groups [1], because Groups [0] is the entire matching string, that is, the content of the entire variable x.
String x = "Live for nothing, die for something ";
Regex r = new Regex (@ "^ Live for no (? <G1> [a-z] {5}), die for some \ 1 $ ");
If (r. IsMatch (x ))
{
Console. WriteLine ("group1 value:" + r. Match (x). Groups ["g1"]. Value); // output: thing
}
// Indexes can be performed based on the group name. Use the following format to identify a group name (? <Groupname> ...).
String x = "Live for nothing ";
Regex r = new Regex (@ "([a-z] +) \ 1 ");
If (r. IsMatch (x ))
{
X = r. Replace (x, "$1 ");
Console. WriteLine ("var x:" + x); // output: Live for nothing
}
// Delete the duplicate "nothing" in the original string ". In addition to the expression, use "$1" to reference the first group. The group name is used for reference below:
String x = "Live for nothing ";
Regex r = new Regex (@"(? <G1> [a-z] +) \ 1 ");
If (r. IsMatch (x ))
{
X = r. Replace (x, "$ {g1 }");
Console. WriteLine ("var x:" + x); // output: Live for nothing
}
String x = "Live for nothing ";
Regex r = new Regex (@ "^ Live for no (? : [A-z] {5}) $ ");
If (r. IsMatch (x ))
{
Console. WriteLine ("group1 value:" + r. Match (x). Groups [1]. Value); // output :( null)
}
// Add "? : "Indicates that this is a" non-capturing group ", that is, the engine will not save the content of this group.
(8) greedy and non-greedy
The engine of the regular expression is greedy. As long as the mode permits, it will match as many characters as possible. Add "? ", You can change the matching mode to non-greedy. See the following example:
String x = "Live for nothing, die for something ";
Regex r1 = new Regex (@ ". * thing ");
If (r1.IsMatch (x ))
{
Console. WriteLine ("match:" + r1.Match (x). Value); // output: Live for nothing, die for something
}
Regex r2 = new Regex (@".*? Thing ");
If (r2.IsMatch (x ))
{
Console. WriteLine ("match:" + r2.Match (x). Value); // output: Live for nothing
}
(9) backtracking and non-backtracking
Use "(?> ...) . Due to the greedy nature of the Regular Expression Engine, in some cases, it will be traced back for matching. See the following example:
String x = "Live for nothing, die for something ";
Regex r1 = new Regex (@ ". * thing ,");
If (r1.IsMatch (x ))
{
Console. WriteLine ("match:" + r1.Match (x). Value); // output: Live for nothing,
}
Regex r2 = new Regex (@ "(?>. *) Thing ,");
If (r2.IsMatch (x) // Mismatch
{
Console. WriteLine ("match:" + r2.Match (x). Value );
}
// In r1 ,". * "because of its greedy feature, it will always match to the end of the string, and then match" thing ", but fails to match", "the engine will trace back at" thing, "Match successful.
In r2, the entire expression fails to be matched due to forced non-backtracking.
(10) forward and reverse pre-Search
Forward pre-search declaration format: positive declaration "(? = ...) ", Negative declaration "(?!...) The statement itself is not part of the final match result. See the following example:
String x = "1024 used 2048 free ";
Regex r1 = new Regex (@ "\ d {4 }(? = Used )");
If (r1.Matches (x). Count = 1)
{
Console. WriteLine ("r1 match:" + r1.Match (x). Value); // output: 1024
}
Regex r2 = new Regex (@ "\ d {4 }(?! Used )");
If (r2.Matches (x). Count = 1)
{
Console. WriteLine ("r2 match:" + r2.Match (x). Value); // output: 2048
}
// The positive declaration in r1 indicates that the four digits must be followed by "used". The negative declaration in r2 indicates that the four digits cannot be followed by "used ".
Reverse pre-search declaration format: positive declaration "(? <=) ", Negative statement "(? <!) The statement itself is not part of the final match result. See the following example:
String x = "used: 1024 free: 2048 ";
Regex r1 = new Regex (@"(? <= Used :) \ d {4 }");
If (r1.Matches (x). Count = 1)
{
Console. WriteLine ("r1 match:" + r1.Match (x). Value); // output: 1024
}
Regex r2 = new Regex (@"(? <! Used :) \ d {4 }");
If (r2.Matches (x). Count = 1)
{
Console. WriteLine ("r2 match:" + r2.Match (x). Value); // output: 2048
}
// The Reverse positive declaration in r1 indicates that the four digits must be followed by "used:". The reverse negative declaration in r2 indicates that the four digits must be followed by "used:.
(11) hexadecimal character range
In a regular expression, you can use "\ xXX" and "\ uXXXX" to indicate a character range ("X" indicates a hexadecimal number:
The characters whose \ xXX number is in the range of 0 to 255. For example, the space can be expressed as "\ x20.
\ UXXXX any character can be expressed by "\ u" plus the 4-digit hexadecimal number of its number. For example, the Chinese character can be expressed by "[\ u4e00-\ u9fa5.
(12) relatively complete matching for [0,100]
The following is a comprehensive example. For matching [0,100], special considerations include:
* 00 legal, 00. Legal, 00.00 legal, 001.100 legal
* The Null String is invalid. Only the decimal point is invalid. The value greater than 100 is invalid.
* The value can be suffixed. For example, "1.07f" indicates that the value is of the float type (not considered)
Regex r = new Regex (@ "^ \ +? 0 *(? : 100 (\. 0 *)? | (\ D {0, 2 }(? = \. \ D) | \ d {1, 2 }(? = ($ | \. $) (\. \ D *)?) $ ");
String x = "";
While (true)
{
X = Console. ReadLine ();
If (x! = "Exit ")
{
If (r. IsMatch (x ))
{
Console. WriteLine (x + "succeed! ");
}
Else
{
Console. WriteLine (x + "failed! ");
}
}
Else
{
Break;
}
}
(13) exact matching is sometimes difficult
In some cases, it is difficult to achieve exact matching, such as date, Url, and Email address. In some cases, you even need to study some specialized documents to write accurate and complete expressions. In this case, you can only return to the next step to ensure exact matching. For example, you can consider a short period of time based on the actual situation of the application system, or for Email-like matching, you can only consider the most common form.
C :\
Yes
Refer to this to clean up the C drive:
1. Disable System Restoration: My computer properties/System Restoration/disable System Restoration on all disks, but I will not be able to use system restoration in the future!
2. Disable System sleep: Control Panel/Power Supply/sleep/remove the check before starting system sleep
3. move the virtual memory, my computer properties/advanced/performance/settings/advanced/change/select the C disk, that is, the system disk, select the no-score page, and then set the virtual memory to its disk, A disk with more disk space remaining, such as D, E, and F. set to 1.5 ~ of memory ~ 2.5 times. The size can be set to the same!
5. Clear temporary IE folders, internet Options, and delete temporary and offline files.
6. delete system logs and program logs, my computer/control panel/management tools/Computer Management/Event Viewer/application, right-click/clear events, and clear system logs in sequence
7. Clear system cache: 2000 all files in the system: C: \ WINNT \ system32 \ dllcache
The XP system is: C: \ windows \ system32 \ dllcache all files under the system cache (open my computer/tool/file and Folder Options/hide the protected system file hook off to hide all files on the hook) ). You can also run the sfc.exe/purgecache command to automatically delete the file.
8. Clear the recycle bin
9. delete the files under c: \ windows \ SoftwareDistribution \ Download (the files downloaded when the system is updated are useless if you have installed the updates)
10. Delete all directories under c: \ windows \ RegisteredPackages
11. Delete all Files under C: \ WINDOWS \ Downloaded Program Files
12. view the hidden files that are known to be protected by the system in my computer folder option, and check all the files.
13. Delete c: \ windows \ All files with $8882305 $ (backup files after system update)
Zhidao.baidu.com/question/11035955.html
Zhidao.baidu.com/question/12223613.html
Zhidao.baidu.com/question/14874715.html
... The remaining full text>
C :\
Yes
Refer to this to clean up the C drive:
1. Disable System Restoration: My computer properties/System Restoration/disable System Restoration on all disks, but I will not be able to use system restoration in the future!
2. Disable System sleep: Control Panel/Power Supply/sleep/remove the check before starting system sleep
3. move the virtual memory, my computer properties/advanced/performance/settings/advanced/change/select the C disk, that is, the system disk, select the no-score page, and then set the virtual memory to its disk, A disk with more disk space remaining, such as D, E, and F. set to 1.5 ~ of memory ~ 2.5 times. The size can be set to the same!
5. Clear temporary IE folders, internet Options, and delete temporary and offline files.
6. delete system logs and program logs, my computer/control panel/management tools/Computer Management/Event Viewer/application, right-click/clear events, and clear system logs in sequence
7. Clear system cache: 2000 all files in the system: C: \ WINNT \ system32 \ dllcache
The XP system is: C: \ windows \ system32 \ dllcache all files under the system cache (open my computer/tool/file and Folder Options/hide the protected system file hook off to hide all files on the hook) ). You can also run the sfc.exe/purgecache command to automatically delete the file.
8. Clear the recycle bin
9. delete the files under c: \ windows \ SoftwareDistribution \ Download (the files downloaded when the system is updated are useless if you have installed the updates)
10. Delete all directories under c: \ windows \ RegisteredPackages
11. Delete all Files under C: \ WINDOWS \ Downloaded Program Files
12. view the hidden files that are known to be protected by the system in my computer folder option, and check all the files.
13. Delete c: \ windows \ All files with $8882305 $ (backup files after system update)
Zhidao.baidu.com/question/11035955.html
Zhidao.baidu.com/question/12223613.html
Zhidao.baidu.com/question/14874715.html
... The remaining full text>