Php 正则表达式,如果没有出现 { } 字符则匹配 [英] Php regex, match if { } characters isn't presented

查看:45
本文介绍了Php 正则表达式,如果没有出现 { } 字符则匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发我自己的用于教育目的的模板引擎.

I'm currently working on my own template engine for educational purposes.

现在我被困在这部分,我想匹配字符串是否包含以下内容,例如:

Now I'm stuck at this part where I want to match if the string contains the following for e.g:

$article.author

到目前为止,这是我的正则表达式模式:http://www.phpliveregex.com/p/fx2

Here's my regex pattern so far: http://www.phpliveregex.com/p/fx2

当前模式匹配两者{$article.author}$article.author.

Current pattern matches both {$article.author} and $article.author.

但它应该只匹配 $article.author.

非常感谢任何帮助,Jumpy.

Any help would be greatly appreciated, Jumpy.

推荐答案

您需要使用锚点来查找完全匹配的内容

You need to use anchors for finding exact match as

^\$[^|{}]*$

正则表达式演示

PHP 代码

$re = "/^\\$[^|{}]*$/m"; 
$str = "\$article.author\n{\$article.timestamp}"; 
preg_match_all($re, $str, $matches);
print_r($matches);

Ideone 演示

这篇关于Php 正则表达式,如果没有出现 { } 字符则匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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