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
中的數字可能會/可能不會使用它,因此我使用了(?:.*)
。
AHHH!有用!正則表達式很混亂,非常感謝你! – 2013-04-04 13:43:12