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

An example Analysis of the look around on the basis of regularity

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shares with you the content of an example analysis of the look around the basis of regularization. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Look around (Lookaround)

1 look around the foundation

Look around only the matching of subexpressions, do not occupy characters, the content of the match is not saved to the final matching result, is zero width. The end result of a look around the match is a location.

The function of looking around is equivalent to adding an additional condition to the position, and only when this condition is satisfied can the look sub-expression match successfully.

According to the direction, there are two kinds of look: order and reverse, and according to whether there are two kinds of positive and negative, there are four kinds of look. A sequential look is equivalent to attaching a condition to the right of the current position, while a reverse look is equivalent to attaching a condition to the left of the current position.

Expression.

Description

(?

Negative look around in reverse order, indicating that the left side of the position cannot match Expression

(= Expression)

The order is sure to look around, indicating that the right side of the location can match Expression.

(?! Expression)

The sequence negates the look around, indicating that the right side of the location cannot match Expression

As for the name of looking around, some documents are called pre-search, and some are called what assertions. Here we use the term "look around" in "proficient regular expressions", which is easily accepted by more people. In fact, it doesn't matter what it is, as long as you know what it is, just a few grammatical rules, it's easy to remember.

2 look around matching principle

Looking around is a difficult point in the regularity. the understanding of looking around can be understood from the perspectives of application and principle. If you want to understand more clearly and deeply, it is better to understand it from the point of view of principle. The basic principle of regular matching refers to the principle of NFA engine matching.

The above-mentioned look is equivalent to attaching a condition to the "location". The difficulty of looking around is to find the "location". Once this is solved, there is no secret to look around.

Sequential look around matching process

For a sequential affirmative look (? = Expression), when the subexpression Expression matches successfully, (? = Expression) matches and reports that (? = Expression) matches the current position successfully.

For the sequential negative look (?! Expression), the (?! Expression) match fails when the subexpression Expression match succeeds; when the subexpression Expression match fails, the (?! Expression) match succeeds and reports that (?! Expression) matches the current position successfully.

The example of sequential positive look has been explained in the NFA engine matching principle, and here we will explain the sequential negative look.

Source string: aa

One

Bb

Two

Cc

Regular expression:] + >

The meaning of this rule is to match division or

The rest of the label.

Matching process:

First, the character ";" is matched by ">".

At this point, the regular expression match is complete and the match is reported to be successful. The matching result is "

The start position is 14 and the end position is 19. Where "match" > ".

Look around the foundation in reverse order

For reverse order, be sure to look around. Failed to match; when the subexpression Expression failed to match, (? Match successfully, and report (? Match the current position successfully

A sequential look is equivalent to attaching a condition to the right of the current position, so its attempt to match starts at the current position, and then tries to match to the right until a location makes the match successful or failed. The special thing about looking around in reverse order is that it is equivalent to attaching a condition to the left of the current position, so it does not start to try to match at the current position, but from a position to the left of the current position until the match is up to the current position. report success or failure of the match.

The starting point of the sequential look around the attempt to match is determined, which is the current position, while the end point of the match is uncertain. The starting point of the matching in reverse order is uncertain, it is a position to the left of the current position, and the end point of the match is determined, that is, the current position.

Therefore, the sequential look is relatively simple, while the reverse look is relatively complex. This is why most languages and tools provide support for sequential look, while only a few languages provide support for reverse look.

Only sequential look is supported in JavaScript, but not reverse order.

Although both sequential look and reverse look are supported in Java, reverse look only supports expressions with definite length, and quantifiers in reverse look only support "?", but not other indefinite length quantifiers. When the length is determined, the engine can look for a fixed length location to the left as the starting point to try to match, and if the length is uncertain, it will try to match one by one from the current position to the left. If it is not successful, it will backtrack, and then try to match to the left position, and then repeat the above process until the match is successful, or after the attempt to position 0, the matching failure will be reported. The complexity of the processing is obvious.

Currently, only .NET supports looking around in reverse order with uncertain length.

Look around the matching process in reverse order

Source string:

A test

Regular expression: (?

At this point, the matching principle of the look around has been basically explained, and there is no secret to speak of. All that is needed is more practice.

3 look around the application

If I am tired of writing today, I will give a comprehensive application example of looking around for the time being. as for the application scenarios and skills of looking around, I will sort it out later.

Requirements: numbers are formatted in currency format with ",".

Regular expression: (n) (?

Test the code:

Double [] data = new double [] {0,12,123,1234, 12345, 123456, 1234567, 123456789, 1234567890, 12.345, 123.456, 1234.56, 12345.6789, 123456.789, 1234567.89, 12345678.9}

Foreach (double d in data)

{

RichTextBox2.Text + = "Source string:" + d.ToString () .PadRight (15) + "format:" + Regex.Replace (d.ToString (), @ "(? n) (?)

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

Database

Wechat

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

12
Report