【问题标题】:Php regex, match if { } characters isn't presentedPhp 正则表达式,如果没有出现 { } 字符则匹配
【发布时间】:2016-05-01 11:21:56
【问题描述】:

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

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

$article.author

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

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

但它应该只匹配$article.author

Jumpy,任何帮助将不胜感激。

【问题讨论】:

  • 使用这个:- ^\$[^\|{}]*$

标签: php regex template-engine


【解决方案1】:

你需要使用anchors来找到完全匹配的

^\$[^|{}]*$

Regex Demo

PHP 代码

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

Ideone Demo

【讨论】:

  • @JumpyN 同样,你不需要在字符类中转义|
【解决方案2】:

这就是你要找的吗?

^\$.*

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    相关资源
    最近更新 更多