Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of finding lookaround before and after regular expression

Shulou Source: shulou.com Published: 2022-06-02 05:14:46 10月03日 Update

This article mainly shows you the "regular expression before and after looking for lookaround example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn the "regular expression before and after looking for lookaround example analysis" this article.

I. introduction of questions

In a HTML page, match the text between a pair of tags, such as the tag of the page, that is, the text between and:

Text: welcome to my page

Regular expression:. *?

Results: [welcome to my page]

Analysis: means case-insensitive, this pattern matches the title tags and the text between them, but not perfectly, because we only want the text between the title tags, not the tags themselves. To solve this problem, we need to use forward and backward search (lookaround).

Second, look forward

Lookup forward specifies a pattern that must match but is not returned in the result. Looking forward is actually a subexpression that starts with? =, and the matching text is followed by =.

Look at an example that matches the protocol portion of an URL address:

Text: http://blog.csdn.net/mhmyqn

Regular expression:. + (? =:)

Results: [http]: / / blog.csdn.net/mhmyqn

Analysis: the protocol part of the URL address is before:, the pattern. + matches any text, the subexpression (? =:) matches:, but what is matched: does not appear in the result. We use? = to indicate to the regular expression engine that all we have to do is find:, but it is not included in the final return result. If you don't use forward matching (? =:) here, but use (:) directly, then the result of the match will be http:, which includes:, which is not what we want.

Note: before and after the search refers to the relative position of the pattern and the searched text, the left is the front and the right is the back. That is, the forward search is: xxx (? = xxx), and the backward search is (?

Text: I paid $30 for 10 apples, 15 oranges, and 10 pears. I saved $5 onthis order.

Regular expression: (? Results: I paid $30 for [10] apples, [15] oranges, and [10] pears. I saved $5 on this order.

Analysis: (?

The above is all the content of the article "sample Analysis of finding lookaround before and after regular expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Text result expression regular pattern analysis tag between price beginning question example content number quantity article part support position metacharacter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL OPPO Reno Redmi Shulou Tech Info