2014-03-05 57 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)