-2

Here's my code,

$msg = "{yellow:red} ERROR:  {blue:light_grey}";
$pattern = "/{(.*)}/";
$a = preg_match_all($pattern, $msg, $regex);

I'm expecting the following 2 matches:

{yellow:red}
{blue:light_grey}

But my result is:

Array
(
    [0] => Array
        (
            [0] => {yellow:red} ERROR:  {blue:light_grey}
        )

    [1] => Array
        (
            [0] => yellow:red} ERROR:  {blue:light_grey
        )

)

Any ideas what's wrong?

Fuxi
  • 7,611
  • 25
  • 93
  • 139

2 Answers2

0

I would change regular to this:

{([^}]*)}

If you want also include {} then

({[^}]*})
Michal Drozd
  • 1,311
  • 5
  • 13
  • 26
  • Do or do not. There is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Sep 30 '20 at 12:49
0

The regex of your desire should be: ({[^}]*}).


$msg = "{yellow:red} ERROR:  {blue:light_grey}";
$pattern = "({[^}]*})";
$a = preg_match_all($pattern, $msg, $regex);

  • ( - The parantheses captures a group

  • { - matches the character { literally (case sensitive)

  • [^ - Match a single character not present in the list

  • * - Matches between zero and unlimited times, in this scenario everything between the given characters. In this scenario: { }

PatricNox
  • 3,306
  • 1
  • 17
  • 25