views:

67

answers:

3

I'm trying to extend PHP Markdown to allow the use of {{ and }} around some parts of the text (they will become search keywords, like a tag).

I have this basic regex that kinda works:

preg_match_all("/\{\{(.*)\}\}/", $description, $nomsTags);

Except it doesn't work when there are 2 keywords in the same sentence:

This {{is}} just an {{example}}.

It returns '{{is}} just an {{example}}' instead of '{{is}}' and '{{example}}'.

How can I modify the regex so it works in both cases?

+2  A: 

Quantifiers are greedy. That means the preceeding expression is expanded to the maximum of possible repetitions. Try a non-greedy quantifier instead by appending a ? to the quantifier:

/\{\{(.*?)\}\}/
Gumbo
Thanks, it worked. I'll accept your answer since it's only 1 character added (I'm lazy ;)) and you put an extra explanation.
Julien Poulin
+2  A: 

Instead of . you could use [^}].

chelmertz
A: 
preg_match_all("/{{([^}]*)}}/", $description, $nomsTags);
Rho