Jumpy N Jumpy N - 2 years ago 64
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:


Here's my regex pattern so far:

Current pattern matches both


But it should only match

Any help would be greatly appreciated, Jumpy.

Answer Source

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);

Ideone Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download