Home Backend Development PHP Tutorial PHP regular expressions - application of greedy mode (or +) and lazy mode (? or +?)

PHP regular expressions - application of greedy mode (or +) and lazy mode (? or +?)

Aug 08, 2016 am 09:22 AM
gt lt php

.表示除\n之外的任意字符; *表示匹配0-无穷; +表示匹配1-无穷;.* — 表示匹配0个或多个处换行符以外的字符;.+ — 表示匹配1个或多个处换行符以外的字符;后边没有?时表示贪婪模式(非懒惰模式);后边多一个?表示懒惰模式;?必须跟在*或者+后边用; 如: 如果用正则匹配src中内容非懒惰模式匹配 src=".*" 匹配结果是:src="test.jpg" width="60" height="80" 意思是从="往后匹配,直到最后一个"匹配结束 懒惰模式正则: src=".*?" 结果:src="test.jpg" 因为匹配到第一个"就结束了一次匹配。不会继续向后匹配。在懒惰模式的正则(.*?或.+?)应用中会出现一种特殊的情况,如:<{ if $test == "test"}> 我是测试数据<{ /if }>如果使用懒惰模式的正则匹配<{和}>里面的内容(当<{和}>的内侧都存在空格,并且这个空格可有可无时),我们一般这样写正则:'/<\{\s*if\s*(.+?)\s*\}>(.+?)<\{\s*\/if\s*\}>/ies' 特别注意其中有颜色的部分,尤其是绿色的部分。当懒惰正则后面紧跟着一个用*修饰的原子时,如果在}>前面存在“可用绿色部分(贪婪)匹配的内容”时,它会以绿色部分匹配到的内容为懒惰模式的匹配结束点,匹配到这里后就结束匹配。如果在}>前面不存在“可用绿色部分(贪婪)匹配的内容”时,它会以绿色部分后面紧跟的一个原子(黄色部分)
为懒惰模式的匹配结束点,匹配到这里后就结束匹配。关于“贪婪模式”和“懒惰模式”,可参考博文“php正则表达式—修正模式”

The above introduces the application of PHP regular expressions - greedy mode (or +) and lazy mode (? or +?), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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 ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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