0

I want to match the string:

test.test ? type == one:prop two:prop

with an regex to get following result:

0 => test.test, 1 => type, 2 => ==, 3 => [[ 0 => one, 1 => prop], [ 0 => two, 1 => prop], ...]

Currently I get only the last match as result with preg_match in PHP 7.3 instead of all matches of the repeating group with the following regex: ([\.a-z]+)\s\?\s([a-z]+)\s(==|!=)\s?(([a-z]+):([\.a-z]+)\s?)+

Check out the tester: https://regex101.com/r/uvATDW/1

How do I have to adapt my regex to achieve the desired result?


I need this regex as part of an retrieving syntax: Consider the array [ test => [ test => [ type => one, prop => value1, proptwo => value2]]]

With the regex result of test.test ? type == one:prop two:proptwo I get the array path test.test.prop depended on the value of type. With this path I can retrieve the value1.

Smeaven
  • 147
  • 3
  • 14
  • 2
    You can't. PCRE does not support a capture stack for each capturing group. If you set a `+` or `*` quantifier to a capturing group, only the last capture will be kept in the capture group memory buffer. It is possible in .NET, Python PyPi regex module, but not in PCRE. Use multiple maching instead or a two-pass approach to extract the full repeated substring with the help of a capturing group around the repeated group and then split into/match the smaller parts with another regex – Wiktor Stribiżew Feb 22 '19 at 08:10
  • I suspected that this would be the case. Two pass makes sense, but what do you mean with multiple matching? – Smeaven Feb 22 '19 at 08:16
  • 1
    I meant a `\G` based regex. Something [like this](https://stackoverflow.com/questions/38475015/match-multiple-times-a-group-only-in-single-regex). – Wiktor Stribiżew Feb 22 '19 at 08:17

0 Answers0