3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('CALCULATION_REGEXP_STRUCTURED_REFERENCE', '([\p{L}_\\\\][\p{L}\p{N}\._]+)?(\[(?>[^\[\]]|(?R))+\])'); $formula = '=SUM(Sales_Data[[#This Row],[Q1]:[Q4]])'; $offset = 5; // This works // No start anchor, but using the preg_match() $offset argument to specify the start point for the search preg_match('/' . CALCULATION_REGEXP_STRUCTURED_REFERENCE . '/misu', $formula, $matches, 0, $offset); var_dump($matches); // This doesn't work // Creating a substring for the part of the formula that we want to search $partialFormula = substr($formula, $offset); var_dump($partialFormula); // and using the start anchor in our regexp preg_match('/^' . CALCULATION_REGEXP_STRUCTURED_REFERENCE . '/misu', $partialFormula, $matches); var_dump($matches);
Output for 7.4.0 - 7.4.30, 8.0.1 - 8.0.20, 8.1.0 - 8.1.7
array(3) { [0]=> string(33) "Sales_Data[[#This Row],[Q1]:[Q4]]" [1]=> string(10) "Sales_Data" [2]=> string(23) "[[#This Row],[Q1]:[Q4]]" } string(34) "Sales_Data[[#This Row],[Q1]:[Q4]])" array(0) { }

preferences:
15.94 ms | 409 KiB | 5 Q