-1

What's the right pattern to obtain something like that using preg_split.

Input:

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

Output:

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

3 Answers3

0

Instead of using preg_split, using preg_match_all makes more sense in this case:

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

Result of $matches:

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

This regex should be fine

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

But instead of preg_split I'd suggest using preg_match_all

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

All matches you're looking for will be bound to $matches[0] array.

matewka
  • 9,912
  • 2
  • 32
  • 43
0

I guess preg_match_all is what you want. This works -

$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)
Kamehameha
  • 5,423
  • 1
  • 23
  • 28