Simple explanation of regular expressions-substitution principle _ Regular expression

Source: Internet
Author: User

The following is an illustrated way to introduce the principle of regular expression substitution, please see below for details.

First, opening

Recently used to replace things so come out and tidy up, here to share is the regular expression inside the substitution principle, first of all to declare that the substitution principle mentioned here is. NET, the principle of substitution of regular expressions. First look at the replacement definition substitution is a language element that can only be recognized in replacement mode. They use the regular expression pattern to define all or part of the text that replaces the matched text in the input string. The substitution pattern can contain one or more substitutions as well as the literal character. In fact, personal summary of the replacement of the roughly, the replacement of the content is always the original text content, through regular expressions to match the text, through the group name or group number for the original text of the replacement, the replacement of the position is a regular expression of the content. And I have written in front of three articles on the regular, and recently used to replace the content, detailed to share to ~, well, nonsense not to say the following into the topic of it ~

Ii. Principle of substitution

. NET below defines the replacement elements for the following table:

  ① $number Replacement Element

The use of these regular expressions and the principle of substitution are explained by a series of examples. First, we want to propose a requirement that the content of the requirement is to match a string of matching digits into a thousand-bit. For example, we now enter a string of 123456789, how to convert it to 123,456,789 of this form. From what I've summed up in the opening, we'll start by matching the two digits of 3, 6, and then replacing 3 with 3, replacing 6 with 6, so the desired effect is achieved, But it's important to note that the following is the premise of matching this number is that the following numbers must be 3 numbers for a group of numbers, such as 3 followed by 456789, (456), (789) is 2 three-digit, after understanding the matching content, start to write regular. We just match one character to the group, and that is the number (\d) is a group or groups of three digits. That will use the zero-width assertion, so that the back of the regular will come out (\d) (? = (\d{3}) + (?!) \d), the regular expression is finished, and then the analysis is replaced. Let's look at what the regular expression matches.

The figure above shows that the entire regular expression match has two matches of 3, and the other matches 6. The pattern of the regular expression is adjusted to replace mode, and the replacement pattern can be replaced with the first method in the table, that is, the group number No. 0 or group 1th can be divided into 3, 6. Take a detailed look at the following image:

In fact, the replacement principle summed up here is based on the matching content to be replaced, because the first image matches two sets of content, the first is replaced with 123,46789, and the second is replaced with a replacement that replaces 6 with 6, which is the content becomes 123, 456,789.

  ②${name} substitution principle

It would be nice to transform the regular expression above, assigning a group number to the first group, so that the entire regular expression becomes this way: Test ' \d ' (? = (\d{3}) + (?! \d), this is the principle of the replacement after this appearance, see the following figure, the principle is the same.

 ③$$ matching Element

$$ match elements are inserted in the matching content of a "$" character, in fact the personal feeling of two $$ is like when the meaning of the conversion character. Converts the meta character $ to a normal character. One of the requirements below is that we match a string of floating-point numbers and convert floating-point numbers to currencies. For example, the amount of money is 12.36,15.66,15.56 this regular expression is also simple, namely ([0-9.] +), look at the following matching content, as shown in the following illustration:

Match the three sets of content, this should be well understood, the following replacement is:

  ④$& substitution Element

The meaning of $& is that the whole regular expression matches the contents of the No. 0 group. The above positive expression can be modified, preceded by a number 5. The regular expression becomes like this 5 ([0-9.] +), then the original text plus a 5, so it won't change too much. The results of the match are as follows:

The following results can be obtained by replacing:

 ⑤$ ' and $ ' replace elements

The two substitution elements are basically the same, but a match is the element of the preceding one. The following elements are matched, and this is explained here according to an example, such as a string of characters containing numbers and because of letters, this time to replace the Chinese characters with the content in front of the position. This time we need to use $ ' for an example to look at this usage: (as shown in the following illustration) ($ ' includes all the text of the input string before the match in the replacement string. )

When we turn on the substitution mode, it replaces the whole expression with what it matches. Let's take a look at the results of the replacements before we analyze them:


Detailed analysis According to the lookup mode, there will be three sets of content to match out, that is, 1,2,3 these three digits, the first replacement is 1 of the overall expression matching content, Replaces 1 with all elements preceding the original text 1 this time the entire string becomes AABBAABBCCDD2EEFF3 note the red position is the replacement position, this will be the next round of replacement, the next round of 2 to replace the original text here has been emphasizing the original text rather than the last replacement of the content, This time we look at the original text 2 before the content is AABB1CCDD2EEFF3, this is the replacement of the content is AABBAABBCCDDAABB1CCDDEEFF3, this is the red position is the replacement of 2 after the content, 3 The previous content is AABB1CCDD2EEFF3 this time to replace the 3 is this text Aabbaabbccddaabb1ccddeeffaabb1ccdd2eeff, replace the complete.

OK analysis to here is the other principles are the same, and so everyone to analyze it ~ ~, through the above analysis. NET no longer worry about my regular expression replacement with a bad ~ ~ ~

Third, concluding remarks

If there are errors in the above analysis, I hope you will point out that xiaoding thanked him again. Here I would like to say that the MSDN information is still very full, we can learn the preferred or 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.