匹配最里面的大括号与正则表达式或strpos?

编程入门 行业动态 更新时间:2024-10-26 01:16:28
本文介绍了匹配最里面的大括号与正则表达式或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?

本文发布于:2023-11-03 12:23:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号   正则表达式   strpos

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!