PHP preg_replace 有数字组

PHP preg_replace with groups of numbers

好的,这就是我所拥有的

$string=Something something 1234-123'; //can be an arbitrary amount of digits on each side of hyphen $numbers=preg_replace("/.*([0-9]*-[0-9]*).*/","", $string); echo $numbers;

这只是打印出“-123”,应该打印出1234-123。我觉得我只是在这里遗漏了一些非常简单的东西。

应该更多non-greedy我猜

.*?([0-9]*-[0-9]*).*

我会用 preg_match 代替:

$string = 'Something something 1234-123';
preg_match('/\d+-\d+/', $string, $match);
echo $match[0],"\n";

输出:

1234-123