引号之间的文本的正则表达式是什么?


What is the regex for the text between quotes?

好吧,我已经试着看了其他的答案,但无法解决我的问题。下面是代码:

{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}

我需要在引号中获得每秒钟的值(因为"name"值是常数)。我实际上发现我需要在:""之间获取文本,但我无法为此编写正则表达式。

编辑:我做preg_match_all在php。它在:""之间,而不是别人编辑的""

到底为什么要尝试用正则表达式解析JSON ?PHP已经可以正确解析JSON ,并且内置了功能。代码:

<?php
$input = '{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}';
print_r(json_decode($input, true));
?>
输出:

Array
(
    [chg] => -0.71
    [vol] => 40700
    [time] => 11.08.2011 12:29:09
    [high] => 1.417
    [low] => 1.360
    [last] => 1.400
    [pcl] => 1.410
    [turnover] => 56,560.25
)

现场演示。

根据您的语言,您可能需要转义字符或在前面或后面添加正斜杠。但它基本上是:

:"([^"].*?)"

/:"([^"].*?)"/

我已经在groovy中测试过了,如下所示。

import java.util.regex.*;
String test='{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}'
  // Create a pattern to match breaks
  Pattern p = Pattern.compile(':"([^"]*)"');
  // Split input with the pattern
  // Run some matches
  Matcher m = p.matcher(test);
  while (m.find())
      System.out.println("Found comment: "+m.group().replace('"','').replace(":",""));

输出是:

Found comment: -0.71
Found comment: 40700
Found comment: 11.08.2011 12:29:09
Found comment: 1.417
Found comment: 1.360
Found comment: 1.400
Found comment: 1.410
Found comment: 56,560.25
PHP示例

<?php
$subject = '{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}';
$pattern = '/(?<=:")[^"]*/';
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

输出是:

Array ( [0] => Array ( [0] => Array ( [0] => -0.71 [1] => 8 ) [1] => Array ( [0] => 40700 [1] => 22 ) [2] => Array ( [0] => 11.08.2011 12:29:09 [1] => 37 ) [3] => Array ( [0] => 1.417 [1] => 66 ) [4] => Array ( [0] => 1.360 [1] => 80 ) [5] => Array ( [0] => 1.400 [1] => 95 ) [6] => Array ( [0] => 1.410 [1] => 109 ) [7] => Array ( [0] => 56,560.25 [1] => 128 ) ) )