This article mainly introduces regular expressionsZero-width assertions, briefly introduces the concept, classification, and PHP implementation skills and related Notes of zero-width assertions, required Friends can refer to
The example in this article describes the zero-width assertion of regular expressions. Share it with everyone for your reference, the details are as follows:
Preface
I have written an article about regular expressions before (http (://www.jb51.net/article/111359.htm) Regulars are introduced in detail in this article, but the introduction of zero-width assertions is rarely mentioned. Now add this content. In this article, we mainly solve the following problems:
① What is a zero-width assertion and why should we use a zero-width assertion?
② How to use a zero-width assertion
Concept
Zero-width assertion, defined in most places, is used to find things before or after certain content (but not including these content), that is, they are like \b ^ $ \< > Such anchoring is used to specify a location that should meet certain conditions (ie, assertions), 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 before 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 what 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) */
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.
Anti-posterior assertion
What is an anti-posterior assertion, use (?
$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:##JavaScript
Regular expression online testing tool: http://tools. jb51.net/regex/javascript
Regular expression online generation tool:
http://tools.jb51.net/regex/create_reg
The above is the detailed content of Detailed explanation of zero-width assertion examples of regular expressions_regular expressions. For more information, please follow other related articles on the PHP Chinese website!