Reputation: 913
I am trying to disect a certain expression:
column_count:[dropdown]:[2:3:4:6]:[3]
There are 4 parts I need to read out:
1. column_count -> no brackets (using preg_split?)
2. [dropdown]:[2:3:4:6]:[3] -> into 3 parts of "brackets insides"
3. [2:3:4:6] -> this third part needs further disection into 4 different values
So my output would be:
1. string: column_count
2. array([dropdown], [2:3:4:6], [3])
3. array(2, 3, 4, 6)
I guess I need 3 different regular expressions for it, but I am still new with them and I have no idea how to approach these steps.
Any explanation or tip is welcome, I would like to learn it.
Upvotes: 1
Views: 57
Reputation: 89557
The more easy seems to me to describe the full string with more or less details (depending the possible formats) and to use capture groups to isolate main parts separated by semi-colons. Once you do that you only need to use basic manipulations to obtain the items you want:
$str = 'column_count:[dropdown]:[2:3:4:6]:[3]';
$pattern = '~^([^:]+):(\[[^]]*]):(\[[^]]*]):(.*)$~';
if (preg_match($pattern, $str, $m))
$result = [
$m[1],
[ $m[2], $m[3], $m[4] ],
explode(':', trim($m[3], '[]'))
];
var_dump($result);
Note that the pattern mainly uses the negated character class [^...]
to stop the greedy quantifiers.
An other way without regex:
The idea is to transform the string to a csv line. To do it all [
and ]
are translated to "[
and ]"
with strtr
, and the double quote is defined as the enclosure parameter for str_getcsv
. In this way, colons between square brackets are ignored by the parser (since they are now between double quotes).
$p = str_getcsv(strtr($str, ['['=>'"[', ']'=>']"']), ':', '"');
$result2 = [
$p[0],
[ $p[1], $p[2], $p[3] ],
explode(':', trim($p[2], '[]'))
];
var_dump($result2);
Upvotes: 2
Reputation: 12689
Try with preg_split
and regex "/:(?=\[)/"
, it should give you an array like array( 'column_count1', '[dropdown]', '[2:3:4:6]', '[3]' )
. For the last part you can use preg_match_all
and regex \d
( or \d+
).
The (?=\[)
part in regex is lookahead for positive assertion.
Upvotes: 2