A detailed description of the sub-patterns of regular expressions, a detailed _php tutorial

Source: Internet
Author: User

A detailed description of a regular expression in a sub-pattern


First, let's look at a PHP code:

 
  PHP    $timedate ("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 "
";     Print_r ($arr);             Echo "
"; }? >

Show Results:

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

Have you noticed that the result of the display is only one data, that is, the time format that matches the pattern, and if there is only one record, why do you want to save it with an array? Isn't it better to save directly with a string?

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

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

For example, we will change the above code slightly to the following:

 
  PHP    $timedate ("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 "
";     Print_r ($arr);             Echo "
"; }? >

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

Execution Result:

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 group will automatically have a group number, the rule is, from left to right, with the left parenthesis of the group as a flag, the first occurrence of the grouping is group number 1, the second is the 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 backward reference to repeat the search for one of the preceding grouped matching text. For example: \1 for grouped 1 matched text, \2 for grouped 2 matched text, etc.
We can further modify the following code as follows:
 
  PHP    $timedate ("y-m-d h:i:s");     $pattern = "/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/I"    ; $replacement = "\ $time Format: $
The replacement format is: \\1 year \\2 month \\3 day \\4 \\5 minutes \\6 seconds "; Print Preg_replace ($pattern$replacement$time); if (preg_match($pattern,$time,$arr)) { echo "
";         Print_r ($arr);                 Echo "
"; }? >

Attention:

Execution Result:

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



http://www.bkjia.com/PHPjc/1121568.html www.bkjia.com true http://www.bkjia.com/PHPjc/1121568.html techarticle a detailed description of the sub-patterns of regular expressions, the regular expressions in detail first, let's look at a PHP code: PHP $time = Date ("y-m-d h:i:s"); $pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d ...

  • 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.