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 C # regular expression matching engine?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the C# regular expression matching engine". In the daily operation, I believe that many people have doubts about what the C# regular expression matching engine is. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the C# regular expression matching engine?" Next, please follow the editor to study!

Now, let's understand a slightly more complex example through a group structure. Take a look at the following detailed example of the C# regular expression matching engine:

String text = "abracadabra1abracadabra2abracadabra3"; string pat = @ "(# * the start of the group abra # match string abra (# the start of the second group cad # match string cad)? # the end of the second group (optional) # * * the end of the group + # match one or more times" / / ignore the annotation Regex r = new Regex (pat, "x") with the x modifier; / / get the list of group numbers int [] gnums = r.GetGroupNumbers (); / / * match Match m = r.Match (text); while (m.Success) {/ / start for from group 1 (int I = 1; I < gnums.Length) Group +) {Group g = m.Group (gnums [I]); / / get the matching group Console.WriteLine ("Group" + gnums [I] + "= [" + g.ToString () + "]"); / / calculate the starting position and length of this group CaptureCollection cc = g.Captures; for (int j = 0; j < cc.Count) ) {Capture c = cc [j]; Console.WriteLine ("Capture" + j + "= [" + c.ToString () + "] Index=" + c.Index + "Length=" + c.Length);}} / / next match m = m.NextMatch ();}

The output of the detailed example of this C# regular expression matching engine is as follows:

Group1= [abra] Capture0= [abracad] Index=0 Length=7 Capture1= [abra] Index=7 Length=4 Group2= [cad] Capture0= [cad] Index=4 Length=3 Group1= [abra] Capture0= [abracad] Index=12 Length=7 Capture1= [abra] Index=19 Length=4 Group2= [cad] Capture0= [cad] Index=16 Length=3 Group1= [abra] Capture0= [abracad] Index=24 Length=7 Capture1= [abra] Index=31 Length=4 Group2= [cad] Capture0= [cad] Index=28 Length=3

Let's start by looking at the string pat, which contains expressions in pat. * capture starts with * parentheses, and then the expression matches to an abra. The second capture group starts with the second parenthesis, but the * capture group has not finished, which means that the result of the * group match is abracad, while the match result of the second group is only cad. So if you use it? Symbol makes cad an optional match, and the result of the match may be abra or abracad. Then, the * groups end, and the expression is required to match multiple times by specifying the + symbol.

Now let's look at what happens during the matching process. First, you create an instance of the expression by calling the constructor method of Regex and specify various options in it. In this example, because of the comments in the expression, the x option is selected, along with some spaces. Turn on the x option and the expression ignores comments and spaces that are not escaped.

Then, get a list of the numbers of the groups defined in the expression. Of course you can use these numbers explicitly, using a programming method here. This method is also very effective as a way to build a quick index if named groups are used.

The next step is to complete * matches. Test the success of the current match through a loop, and then repeat the group list starting with group 1. The reason for not using group 0 in this example is that group 0 is a perfectly matched string, and group 0 will be used if you want to collect all matching strings as a single string.

We track the CaptureCollection in each group. Normally there can be only one capture per group per match, but in this case the Group1 has two capture:Capture0 and Capture1. If you only need Group1's ToString, you will only get abra, and of course it will match abracad. The value of ToString in the group is the value of a Capture in its CaptureCollection, which is exactly what we need. If you want the whole process to end after matching abra, you should remove the + symbol from the expression and let the regex engine know that we only need to match the expression.

At this point, the study of "what is the C# regular expression matching engine" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report