-1
preg_match_all('/({\$([^}]+)})/', $result, $matches)

Can someone please help decipher the regular expression in this call?

Regular expressions are not my best. As far as I can tell it's looking for matches that look like {$foo} but I think I am missing something?

Emma
  • 27,428
  • 11
  • 44
  • 69
D. Simmons
  • 245
  • 1
  • 8

1 Answers1

0

Here, we have a rather simple expression that passes pretty much all chars, bounded from left with { and $ and bounded from right with }. It collects almost all chars in between:

enter image description here

DEMO

RegEx

If this expression wasn't desired, it can be modified or changed in regex101.com. For example, we can restrict it and fail special chars, if we like:

({\$\w+})

DEMO

enter image description here

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Demo

const regex = /({\$\w+})/gm;
const str = `{\$foo}
{\$foo_090_}
{\$foo_@#\$%_5678}
{\$foo_@#\$%_{+[]:}`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

PHP Test

$re = '/({\$\w+})/m';
$str = '{$foo}
{$foo_090_}
{$foo_@#$%_5678}
{$foo_@#$%_{+[]:}';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
Emma
  • 27,428
  • 11
  • 44
  • 69