从字符串中获取数字(可能以逗号分隔)

问题描述:

string is "Lorem, ipsum 12 dolorem, si amet 58,14" and etc. I need to extract "12" and "58,14". I have no experience with regular expresions. So the question is - how should I do this? :) Language (if this matters) is php5.

字符串是“Lorem,ipsum 12 dolorem,si amet 58,14”等等。我需要提取“12 “和”58,14“。 我没有定期表达的经验。 所以问题是 - 我该怎么做? :) 语言(如果这很重要)是php5。 p> div>

<?php
$foundMatches = array();
$targetString = "Lorem, ipsum 12 dolorem, si amet 58,14";
preg_match_all('/([1-9]\d*|0)(,\d+)?/', $targetString, $foundMatches);
var_dump($foundMatches);
?>

You'll find the needed results at $foundMatches[0].

Edit: Updated regex to not match 123, or 012. If you want those use the older /\d+,?\d*?/.

Useful reading: http://www.regular-expressions.info/