Reputation: 52337
I wonder if this is possible. I have the pattern:
foo:(?<id>\d+)(?::(?<srcid>\d+))*
Now I match on this specimen:
asdasdasd {{foo:1381:2:4:7}}
I get the match:
Full match `foo:1381:2:4:7`
Group `id` `1381`
Group `srcid` `7`
However, is it possible to get a result such as:
Full match `foo:1381:2:4:7`
Group `id` `1381`
Group `srcid` [`2`, `4`, `7`]
I need this to work with multiple matches, e.g. asdasdasd {{foo:1381:2:4:7}} {{foo:1111}}
.
Upvotes: 1
Views: 136
Reputation: 785186
You can use \G
in your PCRE regex to get multiple matches after end of previous match:
(?:{{foo:(?<id>\d+)|(?<!^)\G)(?::(?<srcid>\d+)|}})
\G
asserts position at the end of the previous match or the start of the string for the first match.
Sample Code:
$str = 'asdasdasd {{foo:1381:2:4:7}}';
preg_match_all('/(?:foo:(?<id>\d+)|(?<!^)\G):(?<srcid>\d+)/', $str, $m);
print_r($m['srcid']);
echo $m['id'][0]; // 1381
Output:
Array
(
[0] => 2
[1] => 4
[2] => 7
)
1381
Upvotes: 1