Home > Web Front-end > JS Tutorial > body text

How to use regular backreference backreference

php中世界最好的语言
Release: 2018-03-30 10:27:18
Original
1113 people have browsed it

This time I will show you how to use regular backreference backreference, what are the precautions when using regular backreference reference backreference, the following is a practical case, let's take a look.

In all examplesRegular expressionThe matching result is contained between [and] in the source text. Some examples will be implemented using Java. If it is java itself, it is regular The usage of expressions will be explained in the corresponding places. All java examples are tested under JDK1.6.0_13.

1. Problem introduction

A problem of matching title tags (H1-H6) in HTML pages:

Text:

<body>
<h1>Welcome to my page</H1>
Content is pided into twosections:<br>
<h2>Introduction</h2>
Information about me.
<H2>Hobby</H2>
Information about my hobby.
<h2>This is invalid HTML</h3>
</body>
Copy after login
Copy after login

Regular expression: <[hH][1-6]>.*?

Result:


Welcome to my page

】Content is pided into two sections:

Introduction

Information about me.

Hobby

Information about my hobby.

This is invalid HTML

Analysis: Pattern <[hH][1-6]> matches the beginning of any first-level title tag, and is not case-sensitive. In this example, it matches

,

, matches

, ; Lazy metacharacters are used here to match the text in the tags, otherwise the content from the first start tag to the last end tag will be matched. However, it can be seen from the results that an invalid tag is also matched, namely

, and they cannot be matched at all. To solve this problem, you need to use backreference.

2. Backreference matching

Backreference means that the second half of the pattern refers to the subexpression defined in the first half. As for the use, division and reference of subexpressions, they have been introduced before. Now let’s solve the previous example:

Text:

<body>
<h1>Welcome to my page</H1>
Content is pided into twosections:<br>
<h2>Introduction</h2>
Information about me.
<H2>Hobby</H2>
Information about my hobby.
<h2>This is invalid HTML</h3>
</body>
Copy after login
Copy after login

Regular expression: <[hH]([1-6])>.*?

Result:


Welcome to my page

】Content is pided into two sections:

Introduction

Information about me.

Hobby

Information about my hobby.

This is invalid HTML

Analysis: First match the pattern of the opening title tag<[hH]([1-6]) >, use brackets to treat [1-6] as a subexpression, and the matching end title tag pattern is , where \1 means referencing the first subexpression, that is ([1 -6]), if ([1-6]) matches 1, then \1 also matches 1, if it matches 2, then \1 also matches 2, so the last invalid title tag will not was matched.

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:

How to write a regular expression to match a group of characters

Detailed explanation of the positional matching of regular expressions

The above is the detailed content of How to use regular backreference backreference. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!