Home >Web Front-end >JS Tutorial >How to use regular backreference backreference
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>
Regular expression: <[hH][1-6]>.*?[hH][1-6]>
Result:
Analysis: Pattern <[hH][1-6]> matches the beginning of any first-level title tag, and is not case-sensitive. In this example, it matches
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>
Regular expression: <[hH]([1-6])>.*?[ hH]\1>
Result:
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 [hH]\1>, 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!