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

Detailed explanation of backreference of regular expressions_regular expressions

微波
Release: 2017-06-28 13:49:30
Original
1410 people have browsed it

This article mainly introduces Regular expressionsBacktracking of learning tutorialsReferencebackreference, combined with examples, a detailed analysis of the concept, function and implementation skills of backreference, friends in need You can refer to the following example

This article describes the regular expression backreference backreference. Share it with everyone for your reference, the details are as follows:

In all examples, the regular expression matching results are included between [and] in the source text. Some examples will be implemented using Java. If It is the usage of regular expressions in Java itself, which will be explained in the corresponding place. 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:


Welcome to my page

Content is pided into twosections:

Introduction

Information about me.

Hobby

Information about my hobby.

This is invalid HTML

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 opening tag of any first-level title, and is not case-sensitive. In this example, it matches

,

, Matched

, , ; the lazy metacharacter is used here to match the text in the tag, otherwise it will match the tag starting from the first one to the content between the last closing tags. 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:


Welcome to my page

Content is pided into twosections:

Introduction

Information about me.

Hobby

Information about my hobby.

This is invalid HTML

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 represents the first reference 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 be matched.

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

JavaScriptRegular expression online testing tool:
http://tools.jb51.net/regex/javascript

Regular expression Expression online generation tool:
http://tools.jb51.net/regex/create_reg

The above is the detailed content of Detailed explanation of backreference of regular expressions_regular expressions. 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!