如何在双引号内捕获特定字符串

时间:2014-11-22 12:42:26

标签: regex

我有一组以下格式的字符串,我想捕获双引号内的值 的输入

"icici","1001","50.0"
"hdfc","2001","10.0","20.0"

替代参数的预期输出:

\0 match icici and hdfc
\1 match 1001  and 2001
\2 match 50.0 and 10.0
\3 match 20.0

我尝试了下面的正则表达式,但它不能正常工作,你能帮忙吗?

((?:")([0-9A-Za-z.]+)(?:",?))+

1 个答案:

答案 0 :(得分:0)

试试这个:

/\"([a-z0-9.]+)\"/mi允许字符放入[allow char]

live demo

<强> PHP

$re = "/\"([a-z0-9.]+)\"/mi";
$str = "\"icici\",\"1001\",\"50.0\"\n\"hdfc\",\"2001\",\"10.0\",\"20.0\"";

preg_match_all($re, $str, $matches);
    var_dump( $matches[1]);

 $str="";
    if(count($matches[1])>0) foreach($matches[1] as $k=>$v){
        $str .="\\$k"."->".$v."&nbsp;&nbsp;";
    }
    echo $str ;

<强>输出:

array (size=7)
  0 => string 'icici' (length=5)
  1 => string '1001' (length=4)
  2 => string '50.0' (length=4)
  3 => string 'hdfc' (length=4)
  4 => string '2001' (length=4)
  5 => string '10.0' (length=4)
  6 => string '20.0' (length=4)

\0->icici \1->1001 \2->50.0 \3->hdfc \4->2001 \5->10.0 \6->20.0