C # Regular Expression Accumulation

Source: Internet
Author: User
Thank you: Side South Side http://www.cnblogs.com/kissknife/archive/2008/03/23/1118423.html
Shared with yourself the opportunity to learn

 

------------------------------------- Regular Expression -------------------------------------------

@: String x = "d :\\ my Huang \ My doc"; string y = @ "D: \ My Huang \ My doc "; the two statements are equivalent to the classes and methods used in C #: String STR = "1"; RegEx R1 = new RegEx (@ "\ D "); -- The regular format is put in it. If it is a string of characters, it matches bool result = r1.ismatch (STR) as it is; returns trueismatch () and returns true if it is appropriate, we can only make a rough judgment on the numbers of the pure memory part \ D 0-9. (currently, we do not understand that 1.5 is not counted.) ismatch () cannot be used, because there are 1 and 5. \ D completion set \ W word character, refers to uppercase and lowercase letters, 0-9 digits, underline \ W completion set \ s blank character, including line breaks \ n, carriage returns \ r, tabs \ t, vertical tabs \ V, and page breaks \ f \ s. any character except linefeed \ n […] The format in it is used as a whole and matches a character. For example, [ABC] matches a and B matches C [^…]. Match the character splitting operations not listed in []: String Pattern = "[# $]"; // You can also write it as string pattern = "# | $ "; string input = "11 $8 $8 #99"; string [] rs = RegEx. split (input, pattern); array is 11 8 8 99 note: If you want to match. $ * ^) ([] and other strings need to be escaped, for example, "\ $", but in this case, [$] string I = "Hello, world" is not required "; regEx result = new RegEx ("Hello, world"); // trueresult. ismatch (I) is worth pondering RegEx result = new RegEx ("^ Hello, world $"); // trueregex result = new RegEx ("Hello, worl "); // falsere Gex result = new RegEx ("^ hello, worl $"); // falseregex result = new RegEx ("^ hello, worl "); // true common method string I = "hellohello"; RegEx result = new RegEx ("hello"); result. matches. count (I); // 2 pure memory part: The Count-related {n} matches the previous CHARACTER n times {n,} n times or more than N times {n, m} n to m times? Match the first character 0 or 1 + 1 or more times X 0 or the character ^ that is equal to 0 position indicates that the subsequent character must be at the beginning of the string $ the character before it must be at the end of the string. \ B matches the boundary of a word. Note: the so-called boundary is not only space, but also ,. -# $ % and so on are considered boundary for all elements in a word. (2, D, _) None of the boundary \ B matches a non-word boundary string STR = "hello, everybody, happy everyday "; regEx r = new RegEx (@ "^ hello, ([A-Z] {5}) body, happy \ 1day $"); ([A-Z] {5 }) is a group, and the \ 1 after it is referred to as R. match (STR ). groups [1]. obtain "every" string STR = "zhangranzhang"; RegEx r = new RegEx (@ "^ (? <A> [A-Z] {5}) ran \ 1 $ "); // define a name for the group console. writeline (R. matches (STR ). count); console. writeline (R. match (STR ). groups [1]. value); // use the index value console. writeline (R. match (STR ). groups ["A"]. value); // use the key value string STR = "zhangzhang ran"; RegEx r = new RegEx (@ "([A-Z] {5}) \ 1 "); console. writeline (R. replace (STR, "$1"); replace duplicate, field output Zhang ranregex r = new RegEx (@"(?: [A-Z] {5 })");?: Indicates a non-capturing group. In this case, the engine will not regroup and save it. If the group is not used, it will save resources greedy and non-Greedy. By default, the engine is greedy, but after the symbol "* +" or "other" that indicates the number of words, add? Changed to the greedy string mystr = "everyboy is good boy"; RegEx res = new RegEx (@". * Boy "); console. writeline (res. match (mystr ). value); // output everyboy is good boy non-Greedy RegEx res = new RegEx (@". *? Boy "); console. writeline (res. match (mystr ). value); // The regular expression that outputs the greedy and backtracking of everyboy is used to implement the greedy matching string mystr = "everyboy, is good boy "; regEx res = new RegEx (@". * Boy, "); in the above expression, the engine first scans the entire string. The current three-character boy match is actually moving to the end to match the last boy but is matching, when there is a problem, you can only trace back to the latest boy and then match, so the output content is everyboy, (?> ...) This is the RegEx res = new RegEx (@ "(?>. *) Boy, "); this is non-backtracking, so that the matching will fail. ismatch () returns false. Forward pre-search and reverse pre-search forward pre-search include: declare (? =), Negative statement (?!) String STR = "I love the cute nation the big world"; RegEx r = new RegEx (@ ". {3, 4} \ B (? = World) "); Replace (?! World) means that the string following the world match is not followed by the reverse pre-search positive declaration (? <=), Negative statement (? <!)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.