$re = '/\$(?P<type>(?:v|a|s))?(?P<prop>(?:lang|lang2|lang3|language|codec|format|ch|channels|res|resolution))(?:(?:_(?P<num>\d+))|(?:_(?P<lang>[^_0-9\$\n]+)(?:_(?P<num2>\d+))?))?\$/mi';
$str = '$alang$
$alang_0$
$acodec$
$acodec_0$
$acodec_jp$
$acodec_jp_0$
$aformat$
$aformat_0$
$aformat_jp$
$aformat_jp_0$
$ach$
$ach_0$
$ach_jp$
$ach_jp_0$
$achannels$
$achannels_0$
$achannels_jp$
$achannels_jp_0$
$codec$
$res$ $vformat$ $aformat_jp$ $ach_0$';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php