2010-09-03 57 views
0

我的源字符串是這樣的:的Preg匹配所有的貪婪與例外字符串

{categories group_id="3"} 
{category_name} 
{/categories} 

{categories group_id="4"} 
{category_name} 
{/categories} 

我的正則表達式是這樣的:

preg_match('/{categories group_id="3"}(.*){\/categories}/s', $tagdata, $matches); 

導致:

Array 
(
    [0] => Array 
     (
      [0] => {categories group_id="3"} 
    {category_name} 
    {/categories} 
    {categories group_id="4"} 
    {category_name} 
    {/categories} 
     ) 

    [1] => Array 
     (
      [0] => 
    {category_name} 
    {/categories} 
    {categories group_id="4"} 
    {category_name} 

     ) 
) 

你可以看到貪婪太貪婪,一直到二審結束。我很期待是這樣的:

Array 
(
    [0] => Array 
     (
      [0] => {categories group_id="3"} 
    {category_name} 
    {/categories} 
     ) 

    [1] => Array 
     (
      [0] => 
    {category_name} 

     ) 
) 

回答

3
.* greedy 
.*? non-greedy 
+0

哇。我覺得很愚蠢。謝謝。 – Rob 2010-09-03 19:40:10