help with regex expression for PHP
Posted: Tue Feb 09, 2010 2:29 am
I'm trying to write a regex expression to match variables in a MediaWiki template for a forms extension. The variable in a MediaWiki template can take the following forms:
{{{1}}} --- need to capture "1"
{{{var}}} --- need to capture "var"
{{{var|}}} --- need to capture "var"
{{{var| }}} --- need to capture "var"
{{{var| undefined}}} --- need to capture "var"
{{{var word| undefined}}} --- need to capture "var word"
I've got an expression that's matching everything but the last two options; when I write an expression that catches the fourth option, I lose the first option.
Here's what I have so far:
$b = preg_match_all('/\{\{\{([\w]*?)[\s+]?[\|+]?[\s+]?[a-zA-Z0-9_]?\}\}\}/', $a, $matches);
where \{\{\{([\w]*?)[\s+]?[\|+]?[\s+]?[a-zA-Z0-9_]?\}\}\} is my regex.
Any help provided would be most greatly appreciated. I'd like to do this in one pass if possible, but if I have to write two experssions and merge the results I'll settle for that.
Thank you!
{{{1}}} --- need to capture "1"
{{{var}}} --- need to capture "var"
{{{var|}}} --- need to capture "var"
{{{var| }}} --- need to capture "var"
{{{var| undefined}}} --- need to capture "var"
{{{var word| undefined}}} --- need to capture "var word"
I've got an expression that's matching everything but the last two options; when I write an expression that catches the fourth option, I lose the first option.
Here's what I have so far:
$b = preg_match_all('/\{\{\{([\w]*?)[\s+]?[\|+]?[\s+]?[a-zA-Z0-9_]?\}\}\}/', $a, $matches);
where \{\{\{([\w]*?)[\s+]?[\|+]?[\s+]?[a-zA-Z0-9_]?\}\}\} is my regex.
Any help provided would be most greatly appreciated. I'd like to do this in one pass if possible, but if I have to write two experssions and merge the results I'll settle for that.
Thank you!