Drill down to the sub-pattern _ Regular expression of regular expressions

Source: Internet
Author: User

First, we'll look at a section of PHP code:

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

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

Have you noticed that the result of the display is only one piece of data, the time format that matches the pattern, and if there is only one record, why save it with an array? Is it better to use string storage directly?

With this problem, let's look at the sub pattern in the regular expression.

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

For example, we modify the above code slightly, and change it to the following:

<?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 have only modified $pattern, and in matching mode, parentheses are used ()

Execution results:

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

Summary: We can use parentheses to group the entire matching pattern, by default, each grouping automatically has a group number, which is, from left to right, marked by the opening parenthesis of the group, the first grouping is group number 1, the second is group number 2, and so on. Where grouping 0 corresponds to the entire regular expression.
After you have grouped the entire regular match pattern, you can further use the backward reference to repeat the search for a previously matched text of a group. For example: \1 represents grouping 1 matching text, \2 represents grouping 2 matching text, and so on
We can further modify the following code, 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> replaced by: \\1 year \\2 month \\3 day \\4 \\5 minutes \\6 seconds";
  Print Preg_replace ($pattern, $replacement, $time);
  if (Preg_match ($pattern, $time, $arr)) {
    echo "<pre>";
    Print_r ($arr);    
    echo "</pre>";
>

Attention:

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

$time format is: 2012-06-23 03:30:31
replaced by: June 23, 2012 03:30 31 second
Array
(
  [0] => 2012-06-23 03:30:31
  [1] =>
  [2] => [
  3] =>
  [4] =>
  [5] =>
  [6] =>
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.