C # Regular Expression collation memo

Source: Internet
Author: User
Tags manual reference regular expression

For a while, regular expression learning was hot and trendy, and at that time, several regular expression posts could be seen in the Csdn day, and some of the basics were learned with the help of the Forum and the "C # string and regular Expression reference manual" published by Wrox Press. At the same time also for me in Csdn probably earned 1000 points, today, to find the "C # string and regular Expression reference manual", has been missing. Now use the regular time is also relatively few, the previous notes and so on to tidy up, to ambition not to forget.

(1) "@" symbol

The two ows of the table are hot, when the Morning "@" although not a C # regular expression of the "Member", but it is often with C # regular expression out of pairs. "@" means that the string following it is a "verbatim string", which is not very well understood, for example, where two declarations are equivalent:

String x= "D:\\my huang\\my Doc";

String y = @ "D:\My huang\my Doc";

In fact, C # will complain if you follow the following statement, because "\" is used in C # for escaping, such as "\ n" line wrapping:

string x = "D:\My huang\my Doc";

(2) basic grammatical characters.

Number of \d 0-9

\d \d's complement (so that the word identifier complete, the same below), that is, all non-numeric characters

\w A word character, a number of uppercase and lowercase letters, 0-9, underline

The complement of \w \w

\s White-space characters, including line breaks \ n, carriage returns \ r, tabs \ T, vertical tab \v, page breaks \f

The complement of \s \s

. Any character other than the newline character \ n

[...] Matches all the characters listed in []

[^ ...] Match characters listed in non []

Some simple examples are provided below:

Code

string i = "\n";
string m = "3";
Regex r = new Regex(@"\D");
//同Regex r = new Regex("\\D");
//r.IsMatch(i)结果:true
//r.IsMatch(m)结果:false
string i = "%";
string m = "3";
Regex r = new Regex("[a-z0-9]");
//匹配小写字母或数字字符
//r.IsMatch(i)结果:false
//r.IsMatch(m)结果:true

(3) Positioning character

"Positioning character" represents a virtual character, which represents a position, you can also intuitively think that "positioning character" represents a character and the small gap between the characters.

^ indicates that subsequent characters must be at the beginning of the string

$ indicates that the character before it must be at the end of the string

\b Match the bounds of a word

\b Matches a non-word boundary

Related Article

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.