【发布时间】:2016-05-05 04:03:50
【问题描述】:
我正在尝试使用 php 查找模式匹配。
我的字符串包含类似这样的内容
{{ @sql( select [name] from [table] where [col1] = 50 AND [col2] = :alt_id_1 AND [col3] = :id ) }}
我需要返回以下数据
- 字符串匹配的部分
-
{{@sql(和)}之间的字符串 - 任何以冒号开头的关键字
:
例如,如果我有以下字符串
$subject = 'Hello This is a test. The value found is "{{ @sql( select [name] from [table] where [col1] = 50 AND [col2] = :alt_id_1 AND [col3] = :id ) }}".';
那么对于第一个子弹,我需要返回{{ @sql( select [name] from [table] where [col1] = 50 AND [col2] = :alt_id_1 AND [col3] = :id ) }}
对于第二个子弹,我需要返回 select [name] from [table] where [col1] = 50 AND [col2] = :alt_id_1 AND [col3] = :id
对于最后一个项目符号,我将返回一个包含 :alt_id_1 和 :id 的数组
这是我尝试过的
$pattern = '/\{\{\s*+@sql(*)+\s*+\}\}/i';
$matches = [];
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
echo '<pre>';
print_r($matches );
echo '</pre>';
但它给了我一个例外
preg_match_all(): Compilation failed: nothing to repeat at offset 13
【问题讨论】:
标签: php regex preg-match preg-match-all