Php regex, match if { } characters isn't presented

烈酒焚心 提交于 2019-12-13 01:28:19

问题


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

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

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

But it should only match $article.author.

Any help would be greatly appreciated, Jumpy.


回答1:


You need to use anchors for finding exact match as

^\$[^|{}]*$

Regex Demo

PHP Code

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

Ideone Demo




回答2:


Is this what you are looking for?

^\$.*

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



来源:https://stackoverflow.com/questions/36966076/php-regex-match-if-characters-isnt-presented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!