Php 正则表达式,如果没有出现 { } 个字符则匹配
Php regex, match if { } characters isn't presented
出于教育目的,我目前正在开发自己的模板引擎。
现在我被困在这部分,如果字符串包含以下内容,我想匹配,例如:
$article.author
到目前为止,这是我的正则表达式模式:
http://www.phpliveregex.com/p/fx2
当前模式与两者都匹配
{$article.author}
和 $article.author
.
但它应该只匹配 $article.author
.
Jumpy,如有任何帮助,我们将不胜感激。
您需要使用锚点来找到完全匹配的
^$[^|{}]*$
PHP代码
$re = "/^\$[^|{}]*$/m";
$str = "$article.author\n{$article.timestamp}";
preg_match_all($re, $str, $matches);
print_r($matches);
这是您要找的吗?
^\$.*
出于教育目的,我目前正在开发自己的模板引擎。
现在我被困在这部分,如果字符串包含以下内容,我想匹配,例如:
$article.author
到目前为止,这是我的正则表达式模式: http://www.phpliveregex.com/p/fx2
当前模式与两者都匹配
{$article.author}
和 $article.author
.
但它应该只匹配 $article.author
.
Jumpy,如有任何帮助,我们将不胜感激。
您需要使用锚点来找到完全匹配的
^$[^|{}]*$
PHP代码
$re = "/^\$[^|{}]*$/m";
$str = "$article.author\n{$article.timestamp}";
preg_match_all($re, $str, $matches);
print_r($matches);
这是您要找的吗?
^\$.*