Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of backtracking reference backreference in regular expression Learning

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of the retrospective citation backreference on regular expression learning. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

I. introduction of questions

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

Text:

Welcome to my pageContent is divided into twosections:

IntroductionInformation about me.HobbyInformation about my hobby.This is invalid HTML

Regular expression:. *?

Results:

[Welcome to my page]

Content is divided into twosections:

[Introduction]

Information about me.

[Hobby]

Information about my hobby.

[This is invalid HTML]

Analysis: the pattern matches the opening tag of any first-level title, and is not case-sensitive, in this case it matches,; lazy metacharacters are used to match the text in the tag. otherwise, it will match the content from the first start tag to the last ending tag. But as you can see from the results, there is an invalid tag that matches, that is, they cannot be matched at all. To solve this problem, you need to use backreference.

Second, backtracking reference matching

A backtracking reference is a subexpression that refers to the second half of the pattern that is defined in the first half. As for the use, division, and reference of sub-expressions, they have been introduced earlier. Now let's deal with the previous example:

Text:

Welcome to my pageContent is divided into twosections:

IntroductionInformation about me.HobbyInformation about my hobby.This is invalid HTML

Regular expression:. *?

Results:

[Welcome to my page]

Content is divided into twosections:

[Introduction]

Information about me.

[Hobby]

Information about my hobby.

This is invalid HTML

Analysis: first match the pattern of the opening title tag, using parentheses to make [1-6] a subexpression, while the matching ending title tag pattern is, where\ 1 refers to the first subexpression, that is, ([1-6]). If ([1-6]) matches 1, then\ 1 also matches 1, and if it matches 2,\ 1 also matches 2. So the last invalid title tag will not be matched.

This is the end of the article on "sample Analysis of backtracking reference backreference in regular expression Learning". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 260

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report