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

An example Analysis of Sub-patterns of regular expressions

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail the example analysis of the sub-patterns of regular expressions. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, let's look at a piece of PHP code:

Display the results:

Array ([0] = > 2012-06-23 03:08:45)

Have you noticed that there is only one piece of data displayed, that is, the time format that matches the pattern, so if there is only one record, why save it in an array? Wouldn't it be better to save it directly with a string?

With this question in mind, let's take a look at the subpatterns in regular expressions.

In regular expressions, you can use "(" and ")" to enclose the substrings in the pattern to form a subpattern. When a subpattern is treated as a whole, it is equivalent to a single character.

For example, let's modify the above code slightly to read as follows:

Note: I only modified $pattern, using parentheses () in the matching pattern.

Execution result:

Array ([0] = > 2012-06-23 03:19:23 [1] = > 2012 [2] = > 06 [3] = > 23 [4] = > 03 [5] = > 19 [6] = > 23)

Summary: we can use parentheses to group the entire matching pattern. By default, each group will automatically have a group number. The rule is, from left to right, marked by the left parenthesis of the group. The first group that appears is group number 1, the second is group number 2, and so on. Where grouping 0 corresponds to the entire regular expression.

Once the entire regular matching pattern has been grouped, you can further use backward references to repeatedly search for matching text in a previous grouping. For example:\ 1 represents matching text in group 1,\ 2 represents matching text in group 2, and so on.

We can further modify the code as follows:

Note:

Because it is in double quotes, you should use two backslashes when grouping, such as:\ 1, and if you are in single quotes, use a backslash, such as:\ 1

\ 1 for capturing the contents of a packet: 2012,\ 6 for capturing the contents of group 6

Execution result:

The format of $time is: 2012-06-23 03:30:31 and the replaced format is: 03:30:31 Array, June 23, 2012 ([0] = > 2012-06-23 03:30:31 [1] = > 2012 [2] = > 06 [3] = > 23 [4] = > 03 [5] = > 30 [6] = > 31) this is the end of the article on "sample Analysis of Sub-patterns of regular expressions" Hope that 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.

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