php—PCRE regular expression assertion
Nov 21, 2016 pm 05:16 PMAn assertion is a test for characters before or after the current matching position. It does not actually consume any characters. Simple assertion codes include b, B, A, Z, z, ^, $, etc. More complex assertions are coded in subgroups. It comes in two types: lookahead assertions (test forward from the current position) and lookbehind assertions (test backwards from the current position).
The matching of an assertion subgroup is still performed in the normal way, but the difference is that it will not cause the current matching point to change. Positive assertions in lookahead assertions (asserting that this match is true) begin with "(?=", and negative assertions begin with "(?!". For example, w+(?=;) matches a word followed by a semicolon but matches The result will not contain a semicolon, foo(?!bar) matches all occurrences of "foo" that are not immediately followed by "bar". Note a similar pattern (?!foo)bar, which cannot be used to find all occurrences of "foo" that are not immediately followed by "bar". "foo" matches "bar", it will find any occurrence of "bar", because the assertion (?!foo) is always TRUE when the next three characters are "bar". What needs to be achieved is this effect.
Positive assertions in lookbehind assertions start with "(?<=", and negative assertions start with "(?<!". For example, (?<!foo)bar uses For finding any "bar" that is not preceded by "foo", the content of the lookbehind assertion is strictly limited to matching fixed-length strings. However, if there are multiple optional branches, they do not need to be the same length. For example, (?<=bullock|donkey) is allowed, but (?<!dogs?|cats?) will cause a compile-time error. It is allowed to match strings of different lengths in the top-level branch. In contrast to Perl 5.005, which requires multiple branches to match strings of the same length (?<=ab(c|de)) such assertions are not allowed because a single top-level branch can match. Two different lengths, but it is acceptable to use two top-level branches (?<=abc|abde) Such assertion implementation, for each optional branch, temporarily moves the current position before the current position that is trying to match at a fixed width. The match fails if there are not enough characters at the moment. Lookbehind assertions can be used to match the end of a string on a one-shot subgroup; an example is giving the end of a string on a one-shot subgroup.
Multiple assertions (in any order) can appear at the same time. For example, (?<=d{3})(?<!999)foo matches the string "foo" that has three numbers in front but is not "999". Note, Each assertion is applied independently to the match at that point in the target string. First it checks that the first three digits are digits, and then checks that the three digits are not "999". This pattern cannot match "foo" which is preceded by three digits. A string of 6 characters with 3 digits other than 999. For example, it does not match "123abcfoo". The pattern that matches the string "123abcfoo" can be (?<=d{3}…)(?<!999. )foo. In this case, the first assertion checks the first 6 characters of (current matching point) and checks that the first three characters are numbers, and then the second assertion checks that the first three characters of (current matching point) are not "999". Assertions can be nested with any complexity. For example, (?<=(?<!foo)bar)baz matches "baz" that has "bar" in front of it but does not have "foo" in front of "bar". Another pattern (?<=d{3}…(?<!999))foo matches "foo" preceded by three numeric characters followed by three any characters that are not 999. Assertion subgroups are non-capturing subgroups and cannot be modified with quantifiers, because it makes no sense to make multiple assertions for the same thing. If all assertions contain a capturing subgroup, then in order to capture the subgroup in the entire pattern For group counting purposes, they are all counted. However, substring capture can only be used for positive assertions, since it is meaningless for negative assertions. Including assertions, the maximum number of subgroups you can have is 200.
Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
