匹配最里面的大括号与正则表达式或strpos? [英] Matching innermost braces with regex or strpos?

查看:298
本文介绍了匹配最里面的大括号与正则表达式或strpos?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种迷你解析语法,我帮助我简化我的视图代码在cakephp。基本上我已经创建了一个表辅助程序,当给定一个数据集和(可选)一组选项,如何格式化数据将渲染一个表,而不是我循环通过数据和手动编辑它。



它允许用户像他们喜欢的一样复杂或简单,它可以变得非常强大。然而,为了实现这一点,我不得不做一个简单的解析语法。作为一个快速示例,用户可以这样做:

  $ this-> Table-> data = $ userData; 
$ this-> Table-> elements ['td'] ['data'] = array(
'{:User.username:}',
' created:}'=> array('Time :: nice')
);
echo $ this-> Table-> render();

然后当渲染表时会生成:

 < table> 
< tbody>
< tr>< td> rich97< / td>< td> Sun 21st 02:30 pm </td> ;</tr>
< / tbody>
< / table>

出现问题时,我尝试嵌套大括号:

  {:User.levels.iconClasses。{:User.access:}:} 

有没有反正我只能得到最内层的括号在第一次循环和循环,直到没有匹配?或者甚至一次去做?




$ b

这是我的正则表达式:

 '/ \ {\:([^}] +)\:\} /'


 解决方案

'/ \ {:([^ {}] +):\} /'


I have a sort of mini parsing syntax I made up to help me streamline my view code in cakephp. Basically I have created a table helper which, when given a dataset and (optionally) a set of options for how to format the data will render out a table, as opposed to me looping though the data and editing it manually.

It allows the user to be as complex or as simple as they like, it can get pretty powerful. However, In order to achieve this I had to make a simple parsing syntax. As a quick example the user would do something like so:

$this->Table->data = $userData;
$this->Table->elements['td']['data'] = array(
    '{:User.username:}',
    '{:User.created:}' => array('Time::nice')
);
echo $this->Table->render();

And when rendering the table would then generate:

<table>
    <tbody>
        <tr><td>rich97</td><td>Sun 21st 02:30pm</td></tr>
    </tbody>
</table>

The problem occurs then I try to nest the braces like so:

{:User.levels.iconClasses.{:User.access:}:}

Is there anyway I can only get the inner most brackets on the first time round and loop though until there are no matches? Or even do it in one go? Or even better use strpos?

Here is my regex as it stands:

 '/\{\:([^}]+)\:\}/'

解决方案

Just add the opening brace to your negated character class:

'/\{:([^{}]+):\}/'

这篇关于匹配最里面的大括号与正则表达式或strpos?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆