Home Backend Development PHP Tutorial Detailed introduction to php-PCRE regular expression back references

Detailed introduction to php-PCRE regular expression back references

Mar 30, 2017 pm 03:03 PM
php

Outside of a character class, a backslash followed by a number greater than 0 (and possibly a single digit) is a backreference to some capturing group that previously appeared in the pattern.

If the number immediately following the backslash is less than 10, it is always a backreference, and an error will be raised if there are not that many capturing groups in the pattern. In other words, the number of quoted parentheses cannot be less than the number of quoted parentheses less than 10. See the "Backslashes" section above to see how numbers are handled.

A backreference will directly match the content actually captured by the referenced capturing group in the target string, rather than matching the content of the subgroup pattern. Therefore, the pattern (sens|respons)e and 1ibility will match "sense and sensibility" and "response and responsibility", but not "sense and responsibility". If case-sensitive matching is forced when backreferencing, for example ((?i)rah)s+1 matches "rah rah" and "RAH RAH", but will not match "RAH rah", even if the original capture sub The group itself is not case sensitive. Annotation: What needs to be considered here is that the content expected by the backward reference is exactly the same as the content obtained by the referenced capture subgroup (of course, we can make it indistinguishable by setting internal options before the backward reference) Case insensitivity can also be achieved by changing the case or adding pattern modifiers. However, this approach actually controls its behavior from the outside.) There may be more than one back reference. References to the same subgroup. A subgroup may not actually be used for a particular match, in which case any backreferences to the subgroup will fail. For example, the pattern (a|(bc))2 always fails when matching strings that begin with "a" but not "bc". Because there may be up to 99 backreferences, any number immediately following the backslash may be a potential backreference count. If the pattern is followed by a numeric character immediately after the backreference, some delimiter must be used to terminate the backreference syntax. This can be done using spaces if the PCRE_EXTENDED option is set. In other cases an empty comment can be used.

If a backreference appears inside the subgroup it refers to, its matching will fail. For example, (a1) will not get any matches. However this reference can be used for internal subpattern repetition. For example, the pattern (a|b1)+ will match any number of strings composed of "a" as well as "aba", "ababba", etc. (Annotation: Because there is an optional path inside the subgroup, there is an optional path The path can complete the matching. After the matching is completed, the back reference can refer to the content). During each iteration of the subpattern, the backreference matches the string that the subgroup matched during the previous iteration. In order to do this work, the pattern must satisfy the condition that the pattern must be able to guarantee that no backreferences need to be matched on the first iteration. This condition can be implemented using an optional path as in the example above, or by modifying the back reference using a quantifier with a minimum value of 0.

As of PHP 5.2.2, the g escape sequence can be used for both absolute and relative references to subpatterns. This escape sequence must be followed by an unsigned number or a negative number, optionally wrapped in parentheses. There is a synonym relationship between sequence 1, g1, and g{1}. This usage eliminates the ambiguity caused by using a backslash followed by a numeric value to describe a backreference. This escape sequence helps distinguish backreferences from octal numeric characters, and also makes it clearer that a backreference is followed by a source-matching digit, such as g{2}1. g An escape sequence followed by a negative number represents a relative backreference. For example: (foo)(bar)g{-1} can match the string "foobarbar", (foo)(bar)g{2} can match "foobarfoo". This is used as an option in long mode to keep track of the subgroup numbers of previous references to a particular subgroup.

Backward references also support syntax description using subgroup names, such as (?P=name) or starting from PHP 5.2.2, k or k’name’ can be used. In addition, support for k{name} and g{name} was added in PHP 5.2.4.

The above is the detailed introduction of php-PCRE regular expression back reference. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles