View. NET programming design from the perspective of Regular Expressions

Source: Internet
Author: User

View programming design from the perspective of Regular Expressions

 

Regular expressions are not only a tool for verification, but also a misunderstanding of regular expressions. Just like before ajax appeared, we only regarded javascript as a "dispensable, decorative" language. actually Regular Expressions

Expressions are full of our programming world. From the compilation and parsing of the lowest compiler to the upper-layer string processing, they are all regular expressions. Let's pay attention to them!

 

In the past, I only thought that regular expressions were a verification tool or used to process some string texts, so I did not learn much about them until I developed a forum similar to Discuz! I pay attention to such forums as NT.

Regular Expressions, especially when generating static pages from the background and templates, it is almost impossible to use regular expressions.

In my ASP. NET Control Development Series has mentioned: After we submit the aspx page, the compiler actually refers to those tags, such as: <asp: button ....... />, use the regular expression mode to parse the entire page

For example, if you see <asp: Button id = "btn1"/>, resolve it to a Button class, and then the entire marked aspx page is displayed, it is parsed as a class. class ending with cs ).

 

Another example is: When we reply to others, we can always see the title, address, and body of the reply email we want to write.

 

The above is just an important example of regular expressions. Let's think about it. The program we write is a lot of string text, program compilation, in fact, the process is based on the corresponding rules.

At the underlying layer, the compiler is using regular expressions to analyze our program. maybe we don't think it works because the compiler does nothing, but we understand the role and mechanism of regular expression.

Later, at least we can be more comfortable with programming.

 

Therefore, when developing websites, such as forums and CMS, it is actually a bunch of strings that don't look at those pages very mysteriously. Maybe we are happy with the development.

Learning regular expressions is not just about learning a few metacharacters. For example, we all know that *,?, The meanings of/s and/w can also be understood by many regular expressions. However, we can understand

Mechanism, write the regular expressions you want to efficiently match? Why do grouping, capturing, and regular expression engines trace back? (You Need To Know some compilation principles)

In the following content. about the common classes and methods of Regular Expressions in. NET (C # description). I hope you can understand the regular expressions before. If you want to understand the regular expressions in depth, I recommend this book "proficient in regular expressions ".

The description is as follows:

1. Quick Start

2. Detailed explanation of core objects

 

Use regular expressions in. NET to import

 

Code
Using System. Text. RegularExpressions;

 

1. Quick Start

1.1 search for matching in strings:

Code
// Check whether a regular expression matches a string.
If (Regex. IsMatch (strString, @ "^ \ s * $ ")
Console. WriteLine ("No, it is empty ")
Else
Console. WriteLine ("Yes, it is not empty ")

// Note that IsMatch has an overloaded method.
// StrString -- the matched text, @ "^ \ s * $" -- the regular expression used for matching
If (Regex. IsMatch (strString, @ "^ \ s * $", RegexOptions. IgnoreCase) // case-insensitive IgnoreCase
Console. WriteLine ("No, it is empty ")
Else
Console. WriteLine ("Yes, it is not empty ")

 

1.2 match and get the matched text:

 

Code
// Match the numbers in the string. If the strString contains digits, the return value is the matched digits.
// Otherwise, the returned result is null.
String result = Regex. Match (strString, "\ d +"). Value;

Note that there are overload Methods
// Of course, the numbers are not case-insensitive,
String result = Regex. Match (strString, "\ d +", RegexOptions. IgnoreCase). Value;

 

1.3 match and get captured text

Code
String result = Regex. Match (strString, @ "^ Subject :(. *)"). Groups [1]. Value;
// Remember the role of the parentheses "()" in the regular expression-grouping and capturing.
// If your string is strString = "Subject: Hello Xiaoyang", the result is "in the brackets ". * "match" Hello Xiaoyang ", and then you can index the matching text in the brackets
// Use Groups. If your regular expression contains more brackets (), then the number in Groups [I] must be changed based on the match string you want.
// If we change the regular expression to ^ Subject :( H )(. *), then, you Groups [1]. the Value is "H", Groups [2]. the Value is "ello Xiaoyang"

// Method is overloaded, that is, add RegexOption

Of course, there are also named captures:

Code
String result = Regex. Match (strString, @ "^ Subject :(? <Sub>. *) "). Groups [" Sub "]. Value;

 

1. 4. search and replace

For example, all a in a string is converted to B.

 

Code
String result = Regex. Replace (strString, @ "^ a $", "B ");

// A more practical example is the conversion of HTML characters, such

String result = Regex. Replace (strString, @ "&", "& amp ");
String result = Regex. Replace (strString, @ "<", "& ait ");

 

The Quick Start is here, and I don't know what you think. If you want me to talk about it. for details about the core object, you can leave a comment and talk about it, because I don't think I have read these articles. it's not easy to understand. If you don't like it, it doesn't make sense to write it out!

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.