Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the replacement principle of regular expressions in .NET?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what is the replacement principle of regular expressions in .NET, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

Substitution principle

The replacement elements of the following table are defined below:

Replace

Description

$number

Includes the last substring matched by the capture group identified by number in the replacement string, where number is a decimal value.

${name}

Includes the last substring in the replacement string matched by the named group specified by (?).

$

Includes a single "$" text in the replacement string.

$&

Includes a copy of the entire match in the replacement string.

$`

Includes all the text of the input string before the match in the replacement string.

$'

Includes all the text of the input string after the match in the replacement string.

$+

Includes the last group captured in the replacement string.

$_

Includes the entire input string in the replacement string.

① $number replacement element

A series of examples are used to explain the usage and substitution principles of these regular expressions. First of all, we want to put forward a requirement, the content of which is to turn a string of matching numbers into a quantile match. For example, we now enter a string of amounts of 123456789, how to convert it into 123456789. From what I summed up at the beginning, first of all, we have to match the two numbers 3pi 6, then replace 3 with 3 and 6 with 6, so that the desired effect will be achieved. but it should be noted that the following, that is, matching this number, is based on the premise that the number must be followed by a string of three digits, for example, 3 is followed by 456789, (456), (789) is two three digits. Once you know what the match is, start writing the rules. If we only match one character into the group, then (\ d) whether the number is followed by one or more sets of three digits. That uses zero-width assertions so that the following regularities come out (\ d) (? = (\ d {3}) + (?!\ d)), and the regular expression is written, and then it's time to analyze and replace it. First take a look at what the regular expression matches.

The figure above shows that there are two matching contents of the entire regular expression, one matching 3 and the other matching 6. Adjust the pattern of the regular expression to the replacement mode, and after replacing the pattern, you can replace it with the first method in the table above, that is, you can divide the number of 310 into either group 0 or group 1. Take a closer look at the following figure:

In fact, the replacement principle summarized here is actually based on the matching content, because there are two groups of matching content in the first picture, and the content after the first set of replacement is 123 46789. The second replacement is based on the previous replacement to replace 6 with 6, this is the content becomes 123456789.

② ${name} replacement principle

Just transform the regular expression above and assign a group number to the first group, so that the whole regular expression looks like this: (? 'test'\ d) (? = (\ d {3}) + (?!\ d)), this is what the post-replacement principle looks like. For details, see the figure below. The principle is the same.

③ $$matching element

The $matching element inserts a "$" character into the matching content, but in fact, it feels like the meaning of the two $$characters. Turn the metacharacter $into a normal character. One of the requirements below is that we match a string of floating-point numbers to convert floating-point numbers into currencies. For example, the regular expression of 12.36, 15.6, 15.6, 15.56 is fairly simple, that is, ([0-9.] +), let's first look at the following matching content, as shown in the following figure:

The matching three sets of content, this should be easy to understand, the following replacement content is:

④ $& replace element

The meaning of $& is the matching content of the entire regular expression, that is, the content of group 0. You can modify the positive expression above and precede it with the number 5. The regular expression becomes like this 5 ([0-9.] +), so the original text is preceded by a 5 so that it doesn't change too much. The matching results are as follows:

The following results can be obtained by replacing:

⑤ $`and $'replace elements

The two replacement elements are basically the same, except that the first element matches the latter element. Let's explain it here according to an example, for example, a string of characters contains numbers and letters. At this time, I want to replace these Chinese characters with the content in front of the location of the Chinese characters. At this point, you need to use $`to look at the use of this according to an example: (shown in the following figure) ($`includes all the text of the input string before the match in the replacement string.)

When we open the replacement mode, it replaces based on what the entire expression matches. Let's take a look at the results of the replacement before analyzing it:

According to the detailed analysis, when looking for the pattern, there will be three groups of content to match, that is, the three numbers 1Magne2 and 3. The first thing to replace is the content that the overall expression matches, replacing 1 with all the elements in front of the original text 1. Then the whole string will become the aabbaabbccdd2eeff3 note that the red position is the replacement position, and then the next round of replacement will be carried out. In the next round of replacing 2 with the original text, what has been emphasized here is the original text rather than the content of the last replacement. At this time, let's take a look at the content in front of the original text 2 is aabb1ccdd2eeff3, this is the content after replacement is aabbaabbccddaabb1ccddeeff3, this is the red position is the content after replacement 2, and the content in front of 3 is aabb1ccdd2eeff3, which is the text aabbaabbccddaabb1ccddeeffaabb1ccdd2eeff after the replacement of 3 at this time.

Thank you for reading this article carefully. I hope the article "what are the replacement principles of regular expressions in .NET" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report