How to create correct RegEx?

I tried many combinations to create right regex, but I failed.

I want to read this, input:

[@Homer Jay Simpson](id:homer) is a good guy.

and get

Array (
   [0] => "[@Homer Jay Simpson](id:homer)",
   [1] => "Homer Jay Simpson",
   [2] => "homer"


After a little bit of fiddling, this regex will work for you:


Used like:

$str = "[@Homer Jay Simpson](id:homer) is a good guy.";
preg_match('#\[@([^\]]+)\]\(id:([^)]+)\)#', $str, $matches);


    [0] => [@Homer Jay Simpson](id:homer)
    [1] => Homer Jay Simpson
    [2] => homer

A tip when creating regex patterns, start small and build onto the ends of it. I started with just matching the inside of the square brackets \[@([^\]]+)\] then pieced on the rest of it. Full log here

preg_match("/\[\@(.*?)\]\(id:(.*?)\)/", $str, $matches);

This will capture what it between brackets [] and ().
\ makes reges read signs as literal.
.*? means lazy capture anything if any length.