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

What are the advanced rules for Flex regular expressions

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what are the advanced rules of Flex regular expressions", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the advanced rules of Flex regular expressions" this article.

Introduction

Flex regular expression (regularexpression) is to use a "string" to describe a feature, and then to verify that another "string" matches this feature. For example, if the expression "ab+" describes a feature as "a" and any "b", then "ab','abb','abbbbbbbbbb'" fits this feature.

Flex regular expressions can be used to:

(1) verify that the string matches the specified characteristics, such as verifying that it is a valid email address.

(2) it is more flexible and convenient to find a string from a long text that meets the specified characteristics than to find a fixed string.

(3) used to replace, more powerful than ordinary replacement.

Flex regular expressions are actually very simple to learn, and a few more abstract concepts are easy to understand. The reason why many people feel that the Flex regular expression is more complex, on the one hand, because most of the documents do not explain from shallow to deep, and the concept does not pay attention to the order, which brings difficulties to the reader's understanding; on the other hand, the documents that come with various engines generally have to introduce its unique features, but this part of the unique features is not the first thing we need to understand.

Some advanced rules in 1.Flex regular expressions

1.1 greed and non-greed in matching times

When using a special symbol that modifies the number of matches, there are several representations that enable the same expression to match different times, such as "{mforce n}", "{m,}", "?", "*", "+", depending on the string being matched. This kind of expression with indefinite number of repeated matches always matches as many times as possible in the matching process. For example, for the text "dxxxdxxxd", an example is as follows:

Expression matching result

(d) (\ w +)

"\ w +" will match all the characters "xxxdxxxd" after "d".

(d) (\ w +) (d)

"\ w +" will match all the characters "xxxdxxx" between * d "and * a" d ". Although "\ w +" can also match with a "d", in order for the entire expression to match successfully, "\ w +" can give up the "d" it could have matched.

Thus it can be seen that when matching, "\ w +" always matches as many characters as possible that conform to its rules. Although in the second example, it does not match a "d", it is also for the entire expression to match successfully. By the same token, expressions with "*" and "{mrecom n}" match as many as possible, with "?" When the expression can be matched but not matched, it is also "to match" as much as possible. This matching principle is called the "greed" mode.

Non-greedy patterns in ◆ Flex regular expressions:

Add a "?" after modifying the special symbol of the number of matches. Number, you can make the expressions with variable matching times match as little as possible, and make the expressions that can match but not match "mismatch" as much as possible. This matching principle is called "non-greedy" mode, also known as "reluctant" mode. If the lack of matching will lead to the failure of the whole expression matching, similar to the greedy pattern, the non-greedy pattern will match a little more to make the whole expression match successfully. For example, for the text "dxxxdxxxd":

Expression matching result

(d) (\ wit?)

"\ wicked?" The characters after "d" will be matched as few as possible, and the result will be "\ wicked?" Only one "x" is matched.

(d) (\ w) (d)

In order for the entire expression to match successfully, "\ wicked?" You have to match "xxx" to match the following "d" so that the entire expression matches successfully. So, the result is: "\ wicked?" Match "xxx"

For more cases, examples are as follows:

Example 1: expression "(. *)" and string "

Aa

Bb

"when matching, the result of the match is: successful; the content of the match is"

Aa

Bb

"the entire string, the"in the expression will match a"in the string."

Example 2: by contrast, the expression "(. *?)" When matching the same string in example 1, you will only get "

Aa

"when you match the next one again, you can get the second one."

Bb

".

1.2 backreference\ 1,\ 2.

When an expression matches, the expression engine records the string matched by the expression contained in parentheses "()". When getting the matching result, the string matched by the expression contained in parentheses can be obtained separately. This has been demonstrated many times in the previous example. In practical applications, when a certain boundary is used to find it, and the content you want to obtain does not contain a boundary, you must use parentheses to specify the desired scope. For example, the front "(. *?)".

In fact, the string matched by the expression contained in parentheses can be used not only after the end of the match, but also during the matching process. The part after the expression can refer to the previous "the submatch in parentheses matches the string that has been matched". The reference method is "\" plus a number. "\ 1" refers to the string matched in the first pair of parentheses, and "\ 2" refers to the string matched in the second pair of parentheses. And so on, if one pair of parentheses contains another pair of parentheses, the outer parentheses are numbered first. In other words, which pair of left parentheses "(" comes first, then the pair will be numbered first.

Examples are as follows:

Example 1: the expression "('|") (. *?) (\ 1) "when matching" 'Hello', "World", the matching result is: successful; the matching content is"' Hello' ". When you match the next one, you can match"World".

Example 2: expression "(\ w)\ 1 {4,}" when matching "aabbbbabcdefgccccc111121111999999999", the matching result is: successful; the matching content is "ccccc". When you match the next one again, you will get 999999999. This expression requires characters in the "\ w" range to be repeated at least 5 times, paying attention to the difference with "\ w {5,}".

Example 3: expression "

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