在php中使用正则表达式时很奇怪[关闭]

I'm trying to detect a pattern using regex but I have a weird problem. That's the pattern -

"/sub\{(.*)\}/i"

I'm using it like this:

if(preg_match("/sub\{(.*)\}/i",$item))
{
    $sub = explode("|",$item);
    $sub_name = explode("{",$sub[0]);
    $sub_name = $sub_name[1];
    ...
}

Here is the string -

sub{selected posts|post[1]}

Well, the string above is good,but when i'm adding another item like this -

sub{selected posts|post[1],post[2]}

Edit - The problem was with the format of my menu, I exploded the "," sign while having it in the sub section Thanks everyone!

You need to escape the curly brackets like this:

"/sub\{(.*)\}/i"

The regular expression looks fine, is there maybe a newline in the part you're trying to math? If so, add the m and s modifiers to get "/sub{(.*)}/ims"

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

In addition to what Mike wrote, make sure to run the string through preg_quote to escape any regex characters.