我目前正在使用自己的模板引擎进行教育。
现在,如果字符串包含以下内容,我就会陷入我想要匹配的部分:
$article.author
到目前为止,这是我的正则表达式模式: http://www.phpliveregex.com/p/fx2
当前模式与两者匹配
{$article.author}
和$article.author
。
但它应该只匹配$article.author
。
任何帮助都会非常感谢,Jumpy。
答案 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)