Php正则表达式,如果{}字符未显示则匹配

时间:2016-05-01 11:21:57

标签: php regex template-engine

我目前正在使用自己的模板引擎进行教育。

现在,如果字符串包含以下内容,我就会陷入我想要匹配的部分:

$article.author

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

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

但它应该只匹配$article.author

任何帮助都会非常感谢,Jumpy。

2 个答案:

答案 0 :(得分:0)

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

^\$[^|{}]*$

<强> Regex Demo

PHP代码

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

<强> Ideone Demo

答案 1 :(得分:-2)

这是你在找什么?

^ \ $ *

http://www.phpliveregex.com/p/fx3