SoFunction
Updated on 2025-03-02

Detailed explanation of sub-patterns of php regular expressions

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

Copy the codeThe code is as follows:

<?php
$time = date ("Y-m-d H:i:s");
$pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/i";
if(preg_match($pattern,$time,$arr)){
echo "<pre>";
print_r($arr);
echo "</pre>";
}
?>

Show results:
Copy the codeThe code is as follows:

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

Have you noticed that the displayed result only has one data, that is, the time format that matches the matching pattern. If there is only one record, why should I save it in an array? Isn't it better to use strings to save directly?

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

In regular expressions, substrings in the pattern can be enclosed using "(" and ")" to form a sub-pattern. When treating a sub-mode as a whole, it is equivalent to a single character.

For example, we slightly modify the above code and change it to the following:
Copy the codeThe code is as follows:

<?php
$time = date ("Y-m-d H:i:s");
$pattern = "/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/i";
if(preg_match($pattern,$time,$arr)){
echo "<pre>";
print_r($arr);
echo "</pre>";
}
?>

Note: I only modified $pattern, and in the matching pattern, I used brackets()

Execution results:

Copy the codeThe code is as follows:

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

Summary: We can use brackets to group the entire matching pattern. By default, each group will automatically have a group number. The rule is to use the left to right, with the left bracket of the group as the mark. The first group appears as group number 1, the second one is group number 2, and so on. Where, grouping 0 corresponds to the entire regular expression. After grouping the entire regular matching pattern, you can further use "reference backward" to repeatedly search for text matching in a previous group. For example: \1 represents the text matching group 1, \2 represents the text matching group 2, etc. We can further modify the code, as shown below:
Copy the codeThe code is as follows:

<?php
$time = date ("Y-m-d H:i:s");
$pattern = "/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/i";
$replacement = "\$time format is: $0<BR>The replaced format is: \\1 year\\February\\3rd\\4:00\5:00\6 seconds";
print preg_replace($pattern, $replacement, $time);
if(preg_match($pattern,$time,$arr)){
echo "<pre>";
print_r($arr);
echo "</pre>";
}
?>

Notice:

Because it is in double quotes, you should use two backslashes when using grouping, such as:\\1, and if it is in single quotes, you should use one backslash, such as:\1
\\1 is used to capture the content of a group: 2012, \\6 is used to capture the content of a group 6
Execution results:


$time format is: 2012-06-23 03:30:31
The replacement format is: June 23, 2012 03:30:31
Copy the codeThe code is as follows:

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