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

Example Analysis of assertion, Patrol, forward assertion and reverse assertion in regular expression

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Xiaobian to share with you the regular expression assertion, inspection, forward assertion, reverse assertion example analysis, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Assertions is difficult to understand in the concept of regular expressions. It usually refers to a test performed at the current matching position of the target string, but this test does not occupy the target string, that is, it does not move the current matching position of the pattern in the target string. As you can see in detail, the regular expression matching parsing process discusses and analyzes (the principle of regular expression matching), which mentions "zero width" a lot of metacharacters, only matching special positions, which can be understood as assertions.

Assertion metacharacter

Common assertion metacharacters are:\ b,\ B,\ A,\ Z,\ z, ^, $they just represent special positions. If there is a string AB, the position is expressed as: 0A1B2.

Metacharacter meaning (described by the position string above) ^ line beginning, string beginning represents position 0$ line end, string tail, indicates position 2\ b word dividing line, can express: 0prime2 position\ B non-word dividing line Can represent the beginning of position 1\ A target (independent of multiline mode) indicates the end of position 0\ Z target or before the newline character at the end (independent of multiline mode) indicates the end of position 2\ z target (independent of multiline mode) indicates position 2\ G the first matching position in the target Aline ZMagnezMagneg is rarely used.

The tests of these assertions are based on the current location, and the assertions support more complex test conditions. More complex assertions are expressed as subpatterns, including advance (forward) assertions (Lookahead assertions) and backward (backward) assertions (Lookbehind assertions). These assertions only make matching conditions and are not recorded in the matching results and do not match characters.

Advance assertion, positive assertion, positive inspection (Lookahead assertions)

The first assertion, which is often expressed (? = pattern), tests whether the matching string is valid from the current matching position, and there are two formats such as (?! pattern). Let's take a look at an example. Source string: "abc100", the regular expression is:

/ [a murz] + (? =\ d +) /, we analyze the following process as shown below:

Control is first taken by the regular expression character [a murz] +, which matches the character "abc", and the position matches from "0" to 3. Test whether / d+ is valid from this location. Matches to the character 100 and returns true. Therefore, the regular expression forward assertion is successful. Returns the matching string "abc"

(?! pattern) it's just a forward match, and when the match fails, it will return true. The following is the system source string: abc100, and the test results are as follows:

Backward assertion, reverse assertion, reverse patrol (Lookbehind assertions)

For subsequent assertions, the common expression is: (?

Control is first taken by the regular expression character /\ abc100 /, which matches the character: "regular", and the position matches from "0" to 6 characters. The left change\ w match failed to be detected from this position 0. So /\ wicked / matches from character b to "bc100", tests it for the character "a" on the left, and asserts that it is correct. So match to the string "bc100", (?

The above is all the contents of the article "sample analysis of assertions, patrols, forward assertions, and reverse assertions in regular expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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