Use regular expressions to view. NET Programming-detailed explanation of Regular Expressions core objects (2)

Source: Internet
Author: User

 

Detailed explanation of core regular expressions (2)

I have been paying attention to this problem when I wrote the article. The layout was good when I wrote the article, but it changed when I posted it. I don't know what's going on, hope you can understand it! Thank you!

In the previous article, we talked about some regular expression classes in. NET, and focused on the Regex class. Next we will explain that today's content is divided into the following parts:

1. Some advanced topics of the Regex class.

First, let's continue with the previous Regex:

1.1 There is a strange method in Regex, that is, its Replace method. Many classes use this method, such as the string class. To put it bluntly, the replacement method is used, however, this method of the Regex class is slightly

Different.

First, let's give an example to facilitate the following:

 

The above example is very simple. Let's take a look at several reloads of the Replace method.

Regex. Replace (strString, replacement );

Regex. Replace (strString, replacement, count );

Regex. Replace (strString, replacement, count, offset );

The Return Value Type of the method is string.

Let's parse the parameter meaning: strString: the string to be replaced, replacement: the string to be replaced, such as strString = "xiaoyang", replacement = "", then

We actually use a space to replace all spaces of strString. count: indicates the number of times you want to match. It is an int type number. By default, if you do not set this value, your regular expression will put the character in strString

Match all the conditions and replace them. If you set this number, the result is "xiaoyang" (Note that there is only one space before and after you do not set this number, there are many ). if you set the count Parameter

If the value is 1, your regular expression only matches and replaces the blank space before strString. The result is "xiaoyang ";

Another parameter is offset. this parameter is the same as the Match () method before and specifies the starting position of the matching.

 

 

As mentioned above, replacement is the string to be replaced. In fact, this is only a usage of replcement, that is, you can use regex. Replace (strString, "xxxx"); "xxxx" to indicate that you use

 

Replacement string. In fact, in the position of the replacement parameter, we can also input a delegate to make usage more flexible.

This method is very useful, for example, but you want to replace some characters in strString, but you will not simply use one, such as "a" to replace a "bc" in strString ", you want to, as long

If you see "bc" in strString, you need to start some operations, and then perform some operations on "bc. in this way, you will see "bc" and then input

One method. Now, passing replacement into a delegate (function pointer) will handle this problem well ..

 

The form of this delegate is as follows:

 

The example we mentioned earlier is delegated as follows:

Regex. Replace (strString, matchEvaluator );

Then you can define the method, such as string MyMatchFun (Match match );

In the MyMatchFun method, the Match object stores information about your Match, such as the "bc" value you just matched, and then matches. value, if there are groups, you also

You can use Groups [I] (Note: I is an array index) to obtain the expected value.

Now let's talk about the advanced topic. Let's take a look at the last Regex method Split.

This method is very similar to the split method of the string class, but this method of the Regex class is matched first and then split. Let's take a look at the example below.

Regex regex

 

First match all "." In the string and then split. The returned result is an array.

The following is the method overload of Split.

Regex. Split (strString );

Regex. Split (strString, count );

Regex. Split (strString, count, offset)

The meaning of the parameter is the same as that of the Replace method above. I will not repeat it here.

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.