30-minute learning regular expression guide

xiaoxiao2021-04-08  484

30-minute learning regular expression guide Jim Hollenhorst (Original: http://www.codeproject.com/dotnet/regextutorial.asp) Learn to use regular expressions within 30 minutes

(Translated others) is really difficult, it seems that you have to strengthen your English.)

Use Expresso to learn .NET regular expressions

You once wanted to know what a regular expression and would you like a basic understanding of it quickly? The purpose of I wrote this article is that you can have a good understanding of the regular expression over 30 minutes. In fact, the regular expression does not look so complicated. The best way to learn is the practice of watching. After you study your first half, you should know some basic constructs, you can design and use regular expressions in your program or web page. In addition to these things that you already like, there are many excellent resources for you to learn.

What is the rule of a regular expression?

Be sure that you must often see the use of "wildcard" in the regular match. For example, if you want to find all the Microsoft Word files in the Windows Directory, use "* .doc" to find because you know the asterisk (*) is a wildcard, is considered to match any sequential string. Regular expressions are further extended on the basis of such a function. In skilled coding or web, it is very frequent and necessary to find strings that match the complex mode. Regular expressions are naturally used to describe such responsibility patterns. As a result, the regular expression is just a simple code of one model. For example, mode "/ w " is a simple way to express "string that matches any non-empty characters". The .NET framework provides a very powerful class that makes you easy to use regular expressions in your application. Using this class library, you can easily find and replace text, decipher complex headers, parsing languages, or verify text. A good way to learn this incredible regular expression syntax is: starting with an instance, then starting your own creation. This guide only introduces the basis of regular expressions and gives a lot of instances in the Expresso class library. Expresso can be used to verify the regular expressions of the instance and your own practice. let's start.

Some simple examples find Elivs assume that you take off all idle times in the document to find ELVIS to be emptive. You can use the following regular expressions:

Elvis Find Elvis

This regular expression is fully valid to find uniform sequential characters. In .NET, you can easily set ignore letters case, once this expression may match "ELVIS", "ELVIS" or "ELVIS". Unfortunately, it will also match the string of the last five characters "Pelvis". We can improve expressions as follows:

2 / Belvis / B puts Elvis as an overall lookup

It's more interesting now. "/ B" is a special code: match the words in the beginning or end. This expression only matches the complete words "ELVIS", of course, ignores the case of the letter. If you look for words "elvis" following "alive" in all rows. The number "." Is a special code that can match the removal line. The asterisk "*" indicates that the previous query criteria can be repeated without limiting. In this case, ". *" Means "matching any characters except the line". So, finding the regular expressions of the ELVIS in the same line to follow the regular expression of Alive.

3 /BELVIS/b.*/balive/b can look for "ELVIS" behind follow "Alive"

Using just a few special characters, we can start building a powerful regular expression, and these expressions we have considered difficult to read. Let's try another example.

Judging the effectiveness of the phone number

Suppose your web page collects the 7-digit phone number of the customer, you want to verify that the number of phone numbers is formatted correctly, "×××××××", each "×" is a number. Below this expression can find such a string: 4 / b / d / d / d / d / d / d lookup 7 digit phone number

Each "/ d" means "matching any single digit". "-" There is no special meaning, just its original literal meaning, matching a connection. In order to avoid annoying repetition, we can use the shortcut symbol representative of the same meaning:

5 / b / d {3} - / d {4} can better find 7 digits of phone numbers

Keeping "{3}" in "/ d" means "repeating the character 3 times". Let us start learning how to test this expression.

Expresso

If you don't find that the regular expression is difficult to read, you are likely to be an idiot expert or coming to another planet. Anyone is impressive about these symbols, including those frequent and test expressions. There are a lot of test tools, but I am more biased to my own tools: Expresso, from CodeProject, the following demonstrated version 2.0. If you want to get a newer version, you can go to http://www.ultrapico.com/. From get, install Expresso, and from the Windows Program Menu Select Guide, you can choose to use each instance by using the "Expression Library" tab.

Figure 1 Expresso Run Example 5

By selecting the first instance, "1.Find Elvis". View the Run Match and then view the tree on the right. Pay attention to there are several matches. Click on each location that matches in the instance. Run a second, third instance, notice that "Pelvis" does not have a related match item. Finally, run the fourth and fifth instances; should see the same number in the text. Try to get off "/ b", you will find postal coding also match the format of the phone number.

.NET regular expression foundation

Let's start learning the regular expression in .NET.

Special characters

At present, you should probably learn some characters with special meaning. You have encountered: "/ b", ".", "*", And "/ d". If you want to match any blank characters, such as spaces, tabs (Tabs), and wrap, you can use "/ s". Similarly, "/ W" matches any text or numeric characters. Let us start some other examples:

6 / ba / w * / b look up the phrase starting with letter A

This looks for the starting position (/ b) of the phrase, then the letter "B", then any repetitive text or numeric character (/ w *), and then the end of the phrase (/ b).

转载请注明原文地址:https://www.9cbs.com/read-132943.html

New Post(0)