2014-03-05 41 views
-1

使用preg_split获取类似内容的正确模式是什么?自定义正则表达式

输入:

 
Src.[VALUE1] + abs(Src.[VALUE2]) 

输出:

Array ( 
    [0] => Src.[VALUE1] 
    [1] => Src.[VALUE2] 
) 
+1

你为什么限制自己'preg_split'?在这种情况下,这似乎不合适。 –

+0

嗨,因为在我的情况下,我使用preg_ *,但如果需要,我可以更改。 – user3383611

回答

0

而不是使用preg_split,用preg_match_all使得在这种情况下,更多的意义:

结果的 $matches
preg_match_all('/\w+\.\[\w+\]/', $str, $matches); 
$matches = $matches[0]; 

Array 
(
    [0] => Src.[VALUE1] 
    [1] => Src.[VALUE2] 
) 
0

此正则表达式应该是罚款但preg_split代替

Src\.\[[^\]]+\] 

我使用preg_match_all

$string = 'Src.[VALUE1] + abs(Src.[VALUE2])'; 
$matches = array(); 
preg_match_all('/Src\.\[[^\]]+\]/', $string, $matches); 

所有匹配你正在寻找将被绑定到$matches[0]阵列建议。

0

我猜preg_match_all是你想要的。这个作品 -

$string = "Src.[VALUE1] + abs(Src.[VALUE2])"; 
$regex = "/Src\.\[.*?\]/"; 
preg_match_all($regex, $string, $matches); 
var_dump($matches[0]); 
/* 
    OUTPUT 
*/ 
array 
    0 => string 'Src.[VALUE1]' (length=12) 
    1 => string 'Src.[VALUE2]' (length=12)