Thanks for the advices and for the alternatives, I finally came up with a solution though. This is the Regular expression that worked for me:
{{\s*(\w*)\s*}}(?=[^%]*%})
Explanation:
{{\s*(\w*)\s*}}
Matches anything inside the {{ }} delimiters including any number of leading or trailing blank spaces inside
(\w*)
matches words with common variables naming convention (i.e. letters, digits or underscores), and puts it in a capture group
(?=[^%]*%})
looks ahead to match ONLY if the condition is true (i.e. it matches only if the {{variable}} is nested inside the {% %} delimiters)
[^%]*
matches each of the rest of the characters in the string to check that ARE NOT a % (percent character)...
%}
matches the end of the {% %} code block
This is the final SINGLE LINE preg_replace() statement achieving the originally desired output:
echo preg_replace('#{{\s*(\w*)\s*}}(?=[^%]*%})#', '\$$1', $string);