Extracting matches from php regex

前端 未结 5 2079
深忆病人
深忆病人 2020-12-03 14:33

In perl regex we can extract the matched variables, ex below.

   # extract hours, minutes, seconds
   $time =~ /(\\d\\d):(\\d\\d):(\\d\\d)/; # match hh:mm         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 14:58

    Try using the named subpattern syntax of preg_match:

    \w+): (?\d+)/', $str, $matches);
    
    // Before PHP 5.2.2, use this:
    // preg_match('/(?P\w+): (?P\d+)/', $str, $matches);
    
    print_r($matches);
    
    ?>
    

    Output:

     Array (
         [0] => foobar: 2008
         [name] => foobar
         [1] => foobar
         [digit] => 2008
         [2] => 2008 )
    

提交回复
热议问题