我有一个字符串,我试图在其中分解为易于处理的数据。对于这个例子,我想要收入和共识数据。
$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
在 中用作明确的模式revenue of $31-34 billion
,数据可能/可能不会使用它,因此我使用了(?:.*)
.