Home >Backend Development >PHP Tutorial > 一个正则表达式的有关问题。

一个正则表达式的有关问题。

WBOY
WBOYOriginal
2016-06-13 13:28:12893browse

请教大家一个正则表达式的问题。。。。。
有一个字符串:[ti:霜华浓][ar:小曲儿][al:][by:文超]
我现在想要分别匹配出:[ti:霜华浓]、[ar:小曲儿]、[al:]、by:文超]

我写的代码如下:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$pattern = '/[ti:.*].*?]/is';
preg_match_all($pattern, $str, $match);
    
echo '<pre class="brush:php;toolbar:false">';
var_dump($match);
echo '
';


打印的结果是:
array(1) {
  [0]=>
  array(5) {
  [0]=>
  string(10) "ti:霜华浓]"
  [1]=>
  string(8) ":小曲儿]"
  [2]=>
  string(2) ":]"
  [3]=>
  string(6) ":文超]"
  [4]=>
  string(14) "t_time:(3:47)]"
  }
}


然后我将正则改成:$pattern = '/\[ti:.*].*?]/is';
匹配出来的结果是:
array(1) {
  [0]=>
  array(1) {
  [0]=>
  string(51) "[ti:霜华浓][ar:小曲儿][al:][by:文超][t_time:(3:47)]"
  }

我哪写错了吗?
正确的应该怎么写呢?

谢谢!
}


------解决方案--------------------
这个意思?
PHP code
$s = '[ti:霜华浓][ar:小曲儿][al:][by:文超]';
preg_match_all('/\[[^]]+\]/', $s, $r);
print_r($r[0]); <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn