Jumpy N Jumpy N - 7 months ago 10
PHP Question

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

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.

Answer

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

Comments