Need help with regular expression

I want to extract parts of string that have a dollar sign followed by digits, and

  • either have a space or = in front of $ and
  • space or end of string after digits

expression in the code below works fine except it doesn’t extract $7. What do I need to change to get

" $1 "
“=$6 "
" $7”

$var = “sometext $1 sometext $2and sometext$4 and sometext($3) sometext=$6 sometext= $7”;
preg_match_all(‘/[=\s]{1}\$\d+[\s$\z]{1}/’,$var,$matches);
var_dump($matches);

Thanks for your help.

Try

preg_match_all(‘~[=\s]\$\d+\s?\b~’,$var,$matches);