PHP:询问preg_match的工作方式

I'm working on adding values into a template and have a problem.

Here is my template

TIME : |time,hour|:|time,min|:|time,sec| <br />
ADDING BLOCK 'TIME' ...<br />
<i>|template,tibcoreapps,main|</i><br />

The characters | is used for seperating blocks of data. When I tried to get block |template,tibcoreapps,main| using preg_match , it didn't work with my code :

preg_match("/(.*)|template,tibcoreapps,(.*)|(.*)/",$templateContent,$result);

var_dump($result);

And result is

array (size=2)
0 => string 'TIME : |time,hour|:|time,min|:|time,sec| <br />  (length=48)
1 => string 'TIME : |time,hour|:|time,min|:|time,sec| <br />  (length=48)

I tried searching around the internet but can't get exactly how preg_match works. I used to think it gets all matched strings if I assign $result ( the third parameter ) . But even when I try to get a block which starting with template,tibcoreapps,, it returns other blocks or the first blocks it gets. Please explain me the way preg_match works.

Thank you for any helps.

Since | has special meaning in regular expressions, you need to escape it.

preg_match("/(.*)\|template,tibcoreapps,(.*)\|(.*)/",$templateContent,$result);

If you're not familiar with regular expressions, you should read the tutorial at regular-expressions.info.