-1

此正则表达式找到正确的字符串,但只返回第一个结果。如何让它搜索文本的其余部分?

$text =",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";

preg_match("/[^,]+[\d+][.?][\d+]*/",$text,$data);

echo $data;

跟进:我正在推动这个脚本的最初期望,并且我正处于提取更多详细数据的地步。浪费了很多时间……有人能解释一下吗?这是我的字符串:

155.101.153.123:simple:mass_mid:[479.0807,99.011, 100.876],mass_tol:[30],mass_mode:  [1],adducts:[M+CH3OH+H],
130.216.138.250:simple:mass_mid:[290.13465,222.34566],mass_tol:[30],mass_mode:[1],adducts:[M+Na],

这是我的正则表达式:“/mass_mid:[((?:\d+)(?:.)(?:\d+)(?:,)*)/”

我真的在这个问题上敲我的头!有人能告诉我如何从结果中排除行mass_mid:[并保留逗号分隔值吗?

4

3 回答 3

2

不要使用正则表达式。用于split分隔逗号上的输入。

正则表达式不是你在碰巧涉及字符串的每个问题上挥舞的魔杖。

于 2013-06-14T19:58:25.753 回答
2

使用preg_match_all而不是preg_match

来自 PHP 手册:

(`preg_match_all`) searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.

After the first match is found, the subsequent searches are continued on from end of the last match.

http://php.net/manual/en/function.preg-match-all.php

于 2013-06-14T18:40:53.417 回答
0

描述

要提取可能包含单个小数点的数值列表,则可以使用此正则表达式

\d*\.?\d+

在此处输入图像描述

PHP 代码示例:

<?php
$sourcestring=",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";
preg_match_all('/\d*\.?\d+/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

产生匹配

$matches Array:
(
    [0] => Array
        (
            [0] => 415.2109
            [1] => 520.33970
            [2] => 495.274100
            [3] => 482.3238
            [4] => 741.5634
            [5] => 655.3444
            [6] => 488.29980
            [7] => 741.5634
        )

)
于 2013-06-15T05:06:59.683 回答