[英]What is the regex for the text between quotes?
Ok, I have tried looking at other answers, but couldn't get mine solved.好的,我已经尝试查看其他答案,但无法解决我的问题。 So here is the code:
所以这里是代码:
{"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"}
I need to get every second value in the quotes (as the "name"
values are constant).我需要在引号中获取每个第二个值(因为
"name"
值是恒定的)。 I actually worked out that I need to get text between :"
and "
but i can't manage to write a regex for that.实际上,我发现我需要在
:"
和"
之间获取文本,但我无法为此编写正则表达式。
EDIT: I'm doing preg_match_all in php.编辑:我在 php 中做 preg_match_all。 And its between
:"
and "
, not "
and "
as someone else edited.它在
:"
和"
之间,而不是其他人编辑的"
和"
之间。
Why on earth would you attempt to parse JSON with regular expressions?你到底为什么要尝试用正则表达式解析JSON ? PHP already parses JSON properly , with built-in functionality.
PHP 已经正确解析 JSON ,具有内置功能。
Code:代码:
<?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));
?>
Output: Output:
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
)
You may need to escape characters or add a forward slash to the front or back depending on your language.您可能需要转义字符或在前面或后面添加正斜杠,具体取决于您的语言。 But it's basically:
但它基本上是:
:"([^"].*?)"
or或者
/:"([^"].*?)"/
I've test this in groovy as below and it works.我已经在 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(":",""));
Output was: Output 是:
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 Example 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);
?>
Output is: Output 是:
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 ) ) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.