Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement Zero width assertion in php regular expression

Shulou Source: shulou.com Published: 2022-06-02 06:40:14 09月12日 Update

This article will explain in detail how to achieve zero-width assertions in php regular expressions. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Concept

Zero-width assertions, defined this way in most places, are used to find things before or after certain content (but not including them), that is, they are like\ b ^ $\

< >

Such anchoring is used to specify a location that should satisfy certain conditions (that is, assertions), so they are also called zero-width assertions. My understanding is to do a match search before or after a string with a defined location. So zero-width assertion, the execution process is divided into two cases, if it is a positive assertion, it should be like this. The first step is to judge whether the assertion is true (that is, whether certain conditions are met). If the conditions are met, then proceed to the next step to find a match. If it is a reverse assertion, the first step is to match in the order of regular expressions. The second step is to meet the reverse endorsement and judge whether it satisfies the negative endorsement.

Positive advance assertion

What is a positive antecedent assertion is to look for a match before the corresponding position of the string, using (? = exp) to match the position before the exp.

Example

$str= "abcgwcab"; $parent='/bc (? = gw) /'; $str=preg_match ($parent,$str,$match); var_dump ($match); / * * output result: int 1array (size=1) 0 = > string 'bc' (length=2) * /

Parsing: first look up the gw position in the string "abcgwcab", assert it to be true, and then match the bc. If you write the regular expression as $parent='/bc (? = gw) ca/';, the match will not succeed.

Anti-antecedent assertion

What is an anti-antecedent assertion? using (?! exp) matching is not followed by exp.

Example:

$str= "abcgwcab"; $parent='/bc (?! ww) gw/';$str=preg_match ($parent,$str,$match); var_dump ($str); var_dump ($match); / * * output: int 1array (size=1) 0 = > string 'bcgw' (length=4) * /

Parsing: first determine whether the string contains bc, then determine that it is not followed by ww, and finally match gw. You can see that before and after the reverse assertion, you can add other matching conditions.

Make a positive and posterior assertion

What is a positive and post assertion, that is, to find a match after the corresponding position of the string, (? String 'ca' (length=2) * /

Parsing: the first step is to find out whether the character "abcgwcab" contains gw, return the result as true, and then proceed to the second step to find out if there is a ca after the gw.

Make an assertion later

What is an anti-post-assertion that uses (?

Str= "abcgwcab"; $parent='/ (?

Tags: Characters strings rules expressions positions numbers conditions content examples articles three letters that is more results order output good practical successful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia macOS Huawei Xiaomi