An example Analysis of backtracking reference backreference in regular expression Learning
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.