I'm working on a MediaWiki extension that needs to read template contents and build an array of variables from the template. The programming language is PHP5 (actually using php 5.2.11, but needs to be backward compatible to php 5.2)
The variables are identified in a template as:
{{{1}}} -- need to capture "1"
{{{var}}} -- need to capture "var"
{{{var|}}} -- need to capture "var"
{{{var|undefined}}} -- need to capture "var"
{{{var| }}} -- need to capture "var"
{{{var name| }}}-- need to capture "var name"
{{{var name}}} -- need to capture "var name"
{{{var name|undefined}}} -- need to capture "var name"
{{{var name |}}} -- need to capture "var name" (note the ending space after var name)
The expression I have so far is:
$b = preg_match_all('/\{\{\{([\w]*?)[\s+]?[\|+]?[\s+]?[a-zA-Z0-9_]?\}\}\}/', $a, $matches);
where
\{\{\{([\w]*?)[\s+]?[\|+]?[\s+]?[a-zA-Z0-9_]*?\}\}\}
is my actual regex string.
I'm not currently able to keep {{{var}}} -- this captured variable is showing up as blank, even though it's getting matched -- the second part of the expression that matches words after the pipe symbol is backtracking over the first match, but I don't know how to write this so that doesn't happen (I hope I'm using the right terminology here -- regular expressions are fairly new for me).
Any assistance provided will be greatly appreciated.
Lisa