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

How to use the selector of regular expression

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the selector of regular expression, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

A "|" in a regular expression indicates a selection. You can use selectors to match one of several possible regular expressions.

If you want to search for the words "cat" or "dog", you can use it. If you want more options, you just need to expand the list.

The selector has the priority of * * in the regular expression, that is, it tells the engine to match either all expressions to the left of the selector or all expressions on the right. You can also use parentheses to limit the scope of selectors. For example, this tells the regular engine to treat (cat | dog) as a regular expression unit.

Principle analysis of selector of regular expression:

Pay attention to the "eagerness to show work" of regular engines

The regular engine is urgent, and when it finds a valid match, it stops searching. Therefore, under certain conditions, the order of the expressions on both sides of the selector will affect the result. Suppose you want to use regular expressions to search a list of functions in a programming language: Get,GetValue,Set or SetValue. One obvious solution is. Let's take a look at the results when searching for SetValue.

Because both and failed, and the match was successful. Because regular-oriented engines are "eager", it returns * successful matches, namely "Set", instead of continuing to search for other better matches.

Contrary to what we expected, the regular expression does not match the entire string. There are several possible solutions. First, considering the "eagerness" of regular engines, change the order of options, such as we use, so that we can give priority to searching for the longest match. We can also combine four options into two options:. Because question mark repeaters are greedy, SetValue is always matched before Set.

A better solution is to use word boundaries: or. Further, since all choices have the same ending, we can optimize the regular expression to.

Thank you for reading this article carefully. I hope the article "how to use the selector of regular expression" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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: 0

*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

Development

Wechat

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

12
Report