Home > Article > Web Front-end > Detailed explanation of the use of regular zero-width assertions based on PHP
This time I will bring you a detailed explanation of the use of PHP regular zero-width assertions. What are the precautions when using PHP regular zero-width assertions? Here are practical cases, let’s take a look.
The example in this article describes the zero-width assertion of regular expression. Share it with everyone for your reference, the details are as follows:
Preface
① What is a zero-width assertion and why should you use a zero-width assertion
② How to use zero-width assertion
Concept
Zero-width assertion is defined like this in most places and is used to find certain content ( But it does not include things before or after these contents), that is to say, their anchoring function like \b ^ $ \< > is used to specify a position, which should meet certain conditions (i.e. assertion) , so they are also called zero-width assertions. My understanding is to perform a matching search before or after a string in a limited position. Therefore, the execution process of zero-width assertions is divided into two situations. If it is a forward assertion, it should be like this. The first step is to judge whether the assertion is true (that is, whether it meets certain conditions). The second step is to proceed to the next step if the conditions are met. Find matches in one step. If it is a reverse assertion, the first step is to match in the order of regular expressions. The second step is to determine whether reverse endorsement is met when encountering reverse endorsement.
Positive lookahead assertion
#What is a positive lookahead assertion is to search and match before the corresponding position in the string, using (?=exp) matching The position in front of exp.
Example
$str="abcgwcab"; $parent='/bc(?=gw)/'; $str=preg_match($parent,$str,$match); var_dump($match); /** 输出结果: int 1 array (size=1) 0 => string 'bc' (length=2) */
Analysis: First find the gw position in the string "abcgwcab", assert it to be true, and then match bc. If the regular expression is written as $parent='/bc(?=gw)ca/'; the match will be unsuccessful.
Anti-lookahead assertion
What is an anti-lookahead assertion? Use (?!exp) to match something that is not followed by exp.
Example:
$str="abcgwcab"; $parent='/bc(?!ww)gw/'; $str=preg_match($parent,$str,$match); var_dump($str); var_dump($match); /** 输出: int 1 array (size=1) 0 => string 'bcgw' (length=4) */
Analysis: First determine whether the string contains bc, then determine whether it is followed by ww, and finally match gw. It can be seen that other matching conditions can be added before and after the reverse assertion.
Assertion before and after
What is assertion before and after, it is to search and match after the corresponding position in the string, (?<= exp) matches the position after exp
Example:
$str="abcgwcab"; $parent='/(?<=gw)ca/'; $str=preg_match($parent,$str,$match); var_dump($str); var_dump($match); /** 输出结果: int 1 array (size=1) 0 => string 'ca' (length=2) */</p> <p style="text-align: left;"> Analysis: The first step is to find whether the character "abcgwcab" contains gw. The return result is true, and then proceed to the second step. Find whether there is ca after gw. </p> <p style="text-align: left;"><span style="font-size: large"><strong>Anti-posterior assertion</strong></span></p> <p style="text-align: left;">What is an anti-posterior assertion, use (?</p> <pre class="brush:php;toolbar:false">$str="abcgwcab"; $parent='/(?<!bc)gw/'; $str=preg_match($parent,$str,$match); var_dump($str); var_dump($match); /** int 0 array (size=0) empty */
Parsing: first in the character Match gw in the string, and then determine whether it is preceded by bc. It is found that it is preceded by bc, so it returns false.
Example 1: \ d+(?=abc)
Determine whether a string contains 'abc' and whether it contains one or more digits in front of itExample 2:
(?<= \d\d)\w
Determine whether there are two consecutive numbers in the string, followed by a letterExample 3:
\d{3} (?! 55)
Determine whether the string contains three consecutive numbers, and the three numbers are not followed by 55Example 4:
(?
Determine whether a string contains a combination of letters and numbers, and it is not preceded by ac
Precede assertion and What is the difference between late assertions?
Previous assertions are regular expressions that are judged before assertions, and assertions are after other regular expressions. Poster assertions are regular expressions that are judged before assertions, and assertions are before other regular expressions. The first and last here are the order of other matching corresponding assertions in the regular expression PS: Here are 2 very convenient regular expression tools for you. Reference usage:
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading: How to implement the fuzzy matching function of regular expressions Using regular expressions to verify and determine passwords Strength and weakness and prompts
The above is the detailed content of Detailed explanation of the use of regular zero-width assertions based on PHP. For more information, please follow other related articles on the PHP Chinese website!