正则表达式过滤 - 模仿Expression Engine的模板解析

I'd like to mimick (reproduce) Expression Engine's fantastic template parsing method. (Pls don't ask me why not using it :))

While i'm able to find and parse simple tags like

{example_param = "param_value"}

i cannot parse tags where closing tag added:

{cyclic_param}
...
{/cyclic_param}

This is the pattern i'm using:

'/[\{^\/](.*)\}/iU'

but it's returns {/cyclic_param} too.

I know there are zillions of regexp tutors out there but this is the thing i cannot understand ever :( (And i cannot figure out from EE's source)

How can i find opening and closing tags (with their inner blocks too) with PHP's regexp?

Thanks for your help!

 preg_match('~{(\w+)}(.+?){/\1}~s', $r, $m);

content will be in $m[2].

this won't handle nesting though.

/edit: complete example

    $text = "
    foo {single1=abc}
    bar {double1} one {/double1}
    foo {single2=def}
    bar {double2} two {/double2}


    ";

    $tag = '~
        {(\w+)}(.+?){/\1}
        |
        {(\w+)=(.+?)}
    ~six';

    preg_match_all($tag, $text, $m, PREG_SET_ORDER);
    foreach($m as $p) {
        if(isset($p[3]))
            echo "single $p[3] with param $p[4]
";
        else
            echo "double $p[1] with content $p[2]
";
    }

I think this is proper for You:

'/\{[^\/]*\}/iU'