Analysis of regular expression-replacement principle (. NET) text and text, regular expression. net

Source: Internet
Author: User

Analysis of regular expression-replacement principle (. NET) text and text, regular expression. net

I. Opening

Recently, we often use replicas to sort out the replacement. Here we will share the replacement principles in regular expressions. First, we need to declare the replacement principles mentioned here.. NET. First, let's take a look at the replacement definition replacement as a language element that can only be recognized in the replacement mode. They use the regular expression mode to define all or part of the text to replace the text matching the text in the input string. The replacement mode can contain one or more replacement and the characters in this document. In fact, the replacement of personal summary is roughly like this. The replacement content will always be the content of the original text, and the text will be matched by a regular expression, to replace the original text by the group name or group number. The replacement location is the content matched by a regular expression. In addition, I have already written three articles about regular expressions. I have recently used the replacement content. For more information, see ~, Okay, I don't need to talk much about it. Let's start with the question below ~

Ii. Replacement principles

. NET defines the replacement elements of the following table:

Replace

Description

$ Number

Including replacement stringNumberThe last substring that the capture group matches.NumberIs a decimal value.

$ { Name }

Including replacement string(? <Name>)The last substring that the specified name group matches.

$

This includes replacing a single "$" text in the string.

$ &

This includes replacing copies of the entire match in the string.

$'

This includes all text of the input string before the match in the replacement string.

$'

This includes all text of the input string after the match in the string is replaced.

$ +

Includes the last group captured in the replacement string.

$ _

This includes replacing the entire input string in the string.

① $ Number replace Element

A series of examples are used to explain the usage and replacement principles of these regular expressions. First, we need to propose a requirement that converts a string of matching numbers into a shard match. For example, if we enter a string of 123456789 yuan, how can we convert it to 123,456,789. From the summary in the beginning, we need to match the numbers 3 and 6, replace 3 with 3, and replace 6 with 6, the desired effect is achieved, however, it should be noted that the following is the prerequisite for matching this number. The following digits must be a string of three numbers. For example, 3 is followed by 456789, (456 ), (789) There are two three-digit numbers. After learning about the Matching content, start to write regular expressions. We only match one character in the group, so the number (\ d) is not followed by a group or multiple groups of three digits. Then we need to use the assertion with Zero Width so that the following regular expression (\ d )(? = (\ D {3}) + (?! \ D), the regular expression is written in this way, and then the analysis is replaced. Let's take a look at the regular expression Matching content.

It indicates that the matching content of the entire regular expression is 3 and 6 respectively. Adjust the regular expression mode to the replacement mode. After the replacement mode, you can use the first method in the table to replace the regular expression, that is, you can divide 0th into 1st or groups. For details, see:

In fact, the replacement principle here is actually replaced based on the Matching content, because there are two groups of content matching the first image, and the content after the first group is 123,46789, the second replacement is to replace 6 with 6 based on the previous replacement, and the content is changed to 123,456,789.

$ { Name }Replacement Principle

Just change the above regular expression and assign a group number to the first group, so that the whole regular expression will look like this :(? 'Test' \ d )(? = (\ D {3}) + (?! \ D). This is the principle of post-replacement. For details, see. The principles are the same.

$Matching Element

$ The Matching Element inserts a "$" character into the Matching content. In fact, the individual feels like two $ characters. Convert metacharacters $ into common characters. The following requirement is that we match a string of floating-point numbers and convert them into currencies. For example, the regular expression of 12.36, 15.66, and 15.56 is relatively simple, that is, ([0-9.] +). First, let's look at the following Matching content, as shown in:

It should be easy to understand the three sets of matched content. The following content should be replaced:

④ $ & Replace Element

$ & Indicates the Matching content of the entire regular expression, that is, the content in the 0th group. You can modify the preceding positive expression and add a number 5 to the front. The regular expression is changed to 5 ([0-9.] +), So adding 5 to the front of the original text won't change too much. The matching result is as follows:

The following result can be obtained through replacement:

$'And $ 'replace Element

The two replacement elements are basically the same, except that the first element matches the later element. Here we will explain it based on an example, for example, if a string of characters contains numbers and letters, you want to replace these Chinese characters with the content before the position of the Chinese characters. In this case, we need to use$'Let's take a look at the usage of this example: (as shown in )($'This includes all text of the input string that replaces the match in the string .)

When the replacement mode is enabled, it replaces the content that matches the entire expression. Let's take a look at the replacement results and analyze them again:

 

The detailed analysis shows that three groups of content match in the search mode, that is, the numbers 1, 2, and 3. First, replace the Matching content of the overall expression 1, replace 1 with all the elements in front of the original text 1. At this time, the entire string is changed to aabbaabbccdd2eeff3. Note that the red position is the replacement position. At this time, the next round of replacement will be performed, in the next round, replace 2 with the original text. Here, we have been emphasizing the original text instead of the previous content. At this time, let's take a look at the content before the original text 2 as aabb1ccdd2eeff3, this is the content after replacement: aabbaabbccddaabb1ccddeeff3; this is the content in red after replacement 2; The content in front of 3 is aabb1ccdd2eeff3. At this time, the text token is replaced after replacement 3.

Okay, so here we can see that other principles are the same. Let's analyze them by yourself ~~, Through the above analysis,. NET is no longer worried about the use of regular expressions ~~~

 

Iii. Conclusion

If any of the above analysis content is incorrect, I hope you can point out that Mr. Ding has thanked me again. Here I want to talk about the wide range of information in Msdn. If you can learn it, the first choice is Msdn.

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.