Home  >  Article  >  Backend Development  >  php—PCRE regular expression assertion

php—PCRE regular expression assertion

伊谢尔伦
伊谢尔伦Original
2016-11-21 17:16:071543browse

An 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 "(?Multiple assertions (in any order) can appear at the same time. For example, (?<=d{3})(?

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, (?<=(?

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.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn