2013-04-04 183 views
0

我有一個字符串,我試圖闖入容易處理的數據。對於這個例子,我想要收入以及共識數據。正則表達式混合貪婪和非貪婪?

$digits = '[\$]?[\d]{1,3}(?:[\.][\d]{1,2})?'; 
$price = '(?:' . $digits . '(?:[\-])?' . $digits . '[\s]?(?:million|billion)?)'; 

$str = 'revenue of $31-34 billion, versus the consensus of $29.3 billion'; 
preg_match_all('/(?:revenue|consensus)(?:.*)' . $price . '/U', $str, $matches[]); 
print_r($matches); 

返回:

Array (
    [0] => Array (
     [0] => Array (
      [0] => 'revenue of $31' 
      [1] => 'consensus of $29' 
     ) 
    ) 
) 

我所期待的:

Array (
    [0] => Array (
     [0] => Array (
      [0] => 'revenue of $31-34 billion' 
      [1] => 'consensus of $29.3 billion' 
     ) 
    ) 
) 

當我離開了U修改:

Array (
    [0] => Array (
     [0] => Array (
      [0] => 'revenue of $31-34 billion, versus the consensus of $29.3 billion' 
     ) 
    ) 
) 

我不能使用of爲d revenue of $31-34 billion中的數字可能會/可能不會使用它,因此我使用了(?:.*)

回答

2
preg_match_all('/(?:revenue|consensus)(?:.*?)' . $price . '/', $str, $matches[]); 
             ^   ^ 

你可以製作一個特別的通配符非貪婪加入?,如.*?。擺脫全球/U修飾符,並將上述通配符更改爲非貪婪,僅剩下$digits$price

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [0] => revenue of $31-34 billion 
        [1] => consensus of $29.3 billion 
       ) 
     ) 
) 
+0

AHHH!有用!正則表達式很混亂,非常感謝你! – 2013-04-04 13:43:12