用于获取变量的正则表达式


Regular expression for fetching variables

我正在尝试获取查询中的变量。变量在查询中被描述为:$$ variable (description) $$,所以我使用这个preg_match_all函数:

preg_match_all("/'$'$'s*[^'$]*'s*'('s*[^'$]*'s*')'s*'$'$/", $queryCode, $variables)

这个查询的一个例子是:

Some code $$ variable (a description) $$ more code $$ variable2 (other description) $$;

但是我没有得到这个查询的结果。你知道我做错了什么吗?

您可以使用这个更简单的正则表达式:

'$'$'h+('S+)'h+'(([^)]+)')

RegEx演示

代码:

$re = '/'$'$'h+('S+)'h+'(([^)]+)')/'; 
$str = "Some code '$'$ variable (a description) '$'$ more code '$'$ variable2 (other description) '$'$;"; 
preg_match_all($re, $str, $matches);