Examples of assertion with zero-width Regular Expressions [based on PHP] and assertion instances with regular expressions
This article describes the assertion of a regular expression with zero width. We will share this with you for your reference. The details are as follows:
Preface
I have previously written an article on regular expressions (http://www.bkjia.com/article/111359.htm) that details regular expressions, but the introduction to zero-width assertion is rarely mentioned. Add this content. This article mainly solves the following problems:
① What is a zero-width assertion? Why should I use a zero-width assertion?
② How to use assertion with Zero Width
Concept
A zero-width assertion is defined in most places as this is used to find things before or after some content (but not including such content, that is to say, they are anchor such as \ B ^ $ \ <>, used to specify a location, which should meet certain conditions (that is, assertions ), therefore, they are also called assertion with zero width. In my understanding, a matching search is performed before or after a character string with a limited position. Therefore, the execution process of the zero-width assertion is divided into two situations. If it is a positive assertion, it should be like this. The first step is to determine whether the assertion is true (that is, whether certain conditions are met) Step 2, if the conditions are met, perform the next search and match. In the case of reverse assertions, the first step is to match in the regular expression order. Step 2: In the event of reverse endorsement, determine whether or not the opposite is true.
Starting line assertion
What is a forward row assertion, that is, search and match before the corresponding position of the string, and use (? = Exp) match the position before exp.
Instance
$ Str = "abcgwcab"; $ parent = '/bc (? = Gw)/'; $ str = preg_match ($ parent, $ str, $ match); var_dump ($ match);/** output result: int 1 array (size = 1) 0 => string 'bc' (length = 2 )*/
Resolution: First, find the gw position in the string "abcgwcab", assert that it is true, and then match bc. If you write the regular expression $ parent = '/bc (? = Gw) ca/'; the matching will fail.
Anti-Predicate
What is anti-first assertion? Use (?! Exp) the matching is not followed by exp.
Instance:
$ Str = "abcgwcab"; $ parent = '/bc (?! Ww) gw/'; $ str = preg_match ($ parent, $ str, $ match); var_dump ($ str); var_dump ($ match);/** output: int 1 array (size = 1) 0 => string 'bcgw '(length = 4 )*/
Resolution: first judge whether the string contains bc, then judge whether it is followed by ww, and finally match gw. We can see that after reverse assertions, other matching conditions can be added.
Post-positive assertions
What is post-positive assertions, that is, search and match after the corresponding position of the string ,(? <= Exp) match the position behind exp
Instance:
$ Str = "abcgwcab"; $ parent = '/(? <= Gw) ca/'; $ str = preg_match ($ parent, $ str, $ match); var_dump ($ str); var_dump ($ match ); /** output result: int 1 array (size = 1) 0 => string 'CA' (length = 2 )*/
Resolution: In the first step, check whether the character "abcgwcab" contains gw and return true. Then, perform the second step to check whether there is a ca after gw.
Anti-post asserted
What is anti-post-development asserted, use (?
$str="abcgwcab";$parent='/(?<!bc)gw/';$str=preg_match($parent,$str,$match);var_dump($str);var_dump($match);/**int 0array (size=0) empty*/
Resolution: first match gw in the string, then judge whether it is bc before it, and find that it is bc before it, so false is returned.
Exercise questions
Example 1:\d+(?=abc)
Determines whether a string contains 'abc' and whether it contains one or more numbers.
Example 2:(?<=\d\d)\w
Determines whether there are two consecutive numbers in the string, and the string is followed by a letter.
Example 3:\d{3} (?!55)
Determines whether the string contains three consecutive numbers, and the value is not 55 after the three numbers.
Example 4:(?<!ac)\w\d
Determines whether a string contains a combination of letters and numbers.
Summary
What is the difference between predicate and post-asserted?
The first assertion is the regular expression before the assertion, and the assertion is after other regular expressions. Post-recognition assertions are regular expressions before assertions, and assertions are before other regular expressions. Here, the first and last are the order in the regular expression corresponding to other matches.
PS: here we will provide two very convenient Regular Expression tools for your reference:
JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript
Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg