doubi1713
doubi1713
2010-11-17 07:28
浏览 69
已采纳

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

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutangu4978
    doutangu4978 2010-11-17 07:30
    已采纳
    <?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/

    点赞 评论