Home Backend Development PHP Tutorial Parsing the shortest matching pattern in regular expressions

Parsing the shortest matching pattern in regular expressions

Sep 21, 2017 am 11:52 AM
match expression

Shortest matching should be used: If there is a piece of text, you only want to match the shortest possible, not the longest. The following article mainly introduces you to the relevant information about the usage of the shortest matching pattern in regular expressions. The introduction in the article is very detailed. Friends in need can refer to it. Let’s take a look together.

Preface

Recently, I wanted to use regular expressions to grab something from a web page. The content was not complicated, but there were many problems. . Not much to say below, let’s take a look at the detailed introduction:

When we use regular expressions to match the beginning and end of a tag, such as matching <h1>hello world</h1> The opening and closing tags of h1 in

Many people may write like this


/<.*h1>/g
Copy after login

But is this really okay?

Because the * matching character matches zero or more of the previous character, and it is a greedy matching

, so you will get the following result.


Obviously this is not what we want, so how to change greedy matching into minimum matching,


/<.*?h1>/g
Copy after login

The above writing method is enough, as shown below:


In fact, the principle should be very simple, because ? is also a greedy match, and can only match 0 to 1,

, so it will end when it matches the first one, thus preventing * from being greedy in matching multiple ones.

The above is the detailed content of Parsing the shortest matching pattern in regular expressions. For more information, please follow other related articles on the PHP Chinese website!

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 Article Tags

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)

Practical Guide to Regular Expressions in Go: How to Match Hexadecimal Color Codes Practical Guide to Regular Expressions in Go: How to Match Hexadecimal Color Codes Jul 13, 2023 am 10:46 AM

Practical Guide to Regular Expressions in Go: How to Match Hexadecimal Color Codes

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

PHP regular expressions: exact matching and exclusion of fuzzy inclusions

PHP regular expression in action: matching letters and numbers PHP regular expression in action: matching letters and numbers Jun 22, 2023 pm 04:49 PM

PHP regular expression in action: matching letters and numbers

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions

How to match in Jedi Submarine 2 How to match in Jedi Submarine 2 Feb 27, 2024 pm 08:43 PM

How to match in Jedi Submarine 2

Type mismatch in Java - java.lang.ClassCastException Type mismatch in Java - java.lang.ClassCastException Jun 24, 2023 pm 09:30 PM

Type mismatch in Java - java.lang.ClassCastException

How to solve Python expression syntax errors? How to solve Python expression syntax errors? Jun 24, 2023 pm 05:04 PM

How to solve Python expression syntax errors?

PHP Regular Expression: How to match all textarea tags in HTML PHP Regular Expression: How to match all textarea tags in HTML Jun 22, 2023 pm 09:27 PM

PHP Regular Expression: How to match all textarea tags in HTML

See all articles