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 Recursive regular expressions in PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you how to use recursive regular expressions in PHP, the content is very detailed, interested friends can refer to, hope to be helpful to you.

When will recursive regular expressions be used? Of course, when a pattern recursively appears in the matching string (seemingly nonsense). The most classic example is that recursive regularization deals with the problem of nested parentheses. Examples are as follows.

Suppose your text contains nested parentheses that match correctly. The depth of parentheses can be an infinite layer. You want to capture such a parenthesis group.

The copy code is as follows:

The result is:

The copy code is as follows:

Array

(

[0] = > (a (b (c) d) e)

[1] = > e

)

It can be seen that the text we need has been captured in $matches [0].

Principle

Now think about the principle.

The key point in the regular expression above is (? r). The function of (? r) is to recursively replace the entire regular expression in which it is located. At each iteration, the PHP parser replaces (? r) with "\ ([^ ()] + | (? r)) *\".

Therefore, in the above example, the regular expression is equivalent to:

The copy code is as follows:

"/\ ([^ ()] + |\ ([^ ()] + |\ ([^ ()] +) *\) /"

But the above code is only suitable for parentheses with a depth of 3 layers. For parenthesis nesting of unknown depth, you have no choice but to use this regularity:

The copy code is as follows:

"/\ (([^ ()] + | (? r)) *\) /"

It not only matches infinite depth, but also simplifies the syntax of regular expressions. Powerful function and concise grammar.

Now let's take a closer look at "/\ ([^ ()] + | (? r)) *\) /" how to match "(a (b (c) d) e)":

The part of "(c)" is matched by the regular expression "([^ ()] +) *\". Please note that (c) is actually a microcosm of the whole recursion. Although the sparrow has all the five organs, it uses the whole regular expression.

In other words, for (c) in the next step, you can use (? r) to match.

The matching process of (b (c) d) is:

"\ (" match "("

"[^ ()] +" match "b"

R) match "(c)"

"[^ ()] +" match "d"

"\)" match ")".

According to the above matching principle, it is not difficult to understand why the second element of the array, $matches [1], is equivalent to'e'. The substring'e'is captured in the last matching iteration. During the matching process, only the last capture is saved to the array.

For this feature, you can try it yourself and see what the capture result of $1 is when you use the regular expression ([a murz] + [0-9] +) + to match the string abc123xyz890. Note that the results do not conflict with the Left Longest principle.

If we only need to capture $matches [0], we can do this:

The copy code is as follows:

The result is the same:

Array

(

[0] = > (a (b (c) d) e)

)

The change is to change capture parentheses () to non-capture parentheses (?:).

It can also be further improved as follows:

The copy code is as follows:

On how to use recursive regular expressions in PHP to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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: 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

Internet Technology

Wechat

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

12
Report