C # Regular Expression classic classification collection page 1/3

Source: Internet
Author: User

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 \ 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:
Copy codeThe 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

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.