Backreference for regular expression Learning

Source: Internet
Author: User

Backreference for regular expression Learning

This article describes how to use a regular expression to reference backreference. We will share this with you for your reference. The details are as follows:

In all examples, the regular expression matching result is included between [and] in the source text. In some examples, Java is used. If the regular expression is used in java, it will be described in the corresponding area. All java examples have passed the test under JDK1.6.0 _ 13.

I. Problem Introduction

A question about matching title tags (H1-H6) on an HTML page:

Text:

<body>

Regular Expression: <[hH] [1-6]> .*? </[HH] [1-6]>

Result:

<Body>
[<H1> Welcome to my page </H1>]
Content is divided into twosections: <br>
[<H2> Introduction Information about me.
[<H2> holobby </H2>]
Information about my holobby.
[<H2> This is invalid HTML </Body>

Analysis: the mode <[hH] [1-6]> matches the start tag of any level-1 title and is case insensitive, in this example, it matches

Ii. backtracking and reference matching

Backend Reference refers to the subexpression defined in the first half of a pattern. The usage, division, and reference of subexpressions have been described earlier. Here we will solve the preceding example:

Text:

<body>

Regular Expression: <[hH] ([1-6])> .*? </[HH] \ 1>

Result:

<Body>
[<H1> Welcome to my page </H1>]
Content is divided into twosections: <br>
[<H2> Introduction Information about me.
[<H2> holobby </H2>]
Information about my holobby.
<H2> This is invalid HTML

Analysis: first, match the start title label mode <[hH] ([1-6])>, and use [1-6] As a subexpression using parentheses, the tag mode of the matched ending title is </[hH] \ 1>, where \ 1 indicates referencing the first subexpression ([1-6]), if ([1-6]) matches 1, \ 1 also matches 1. If it matches 2, \ 1 matches 2, therefore, the last invalid title tag will not be matched.

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.