Home > Web Front-end > JS Tutorial > Detailed explanation of the use of shortest match mode

Detailed explanation of the use of shortest match mode

php中世界最好的语言
Release: 2018-03-29 13:47:53
Original
1209 people have browsed it

This time I will bring you a detailed explanation of the use of the shortest matching mode. What are the precautions when using the shortest matching mode. The following is a practical case, let's take a look.

Preface

Recently I wanted to use regular expression to grab something from the web page, but the content was not It’s complicated but there are 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 follows Picture:

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

so it will match the first It ends when one is used, thus preventing * from greedily matching multiple ones.

PS: Here are two very convenient regular expression tools for your reference:

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Using Linux grep and regular expressions

Using regular expressions to verify input on the login page content

The above is the detailed content of Detailed explanation of the use of shortest match mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template