我有一个列,其中表单数据放在json字符串中,我如何使用查询提取数据。我已经通过互联网检查了解决方案,每个人建议应用正则表达式。我如何应用正则表达式从字符串下面获取Pass Type
的值。
.....{\"label\":\"Pass Type\",\"value\":\"prof_3Day\",\"identifier\":\"field102\",\"type\":\"dropdown\",\"page\":3,\"page_name\":\"Step 3\",\"options\":[{......
答案 0 :(得分:1)
由于所有可能的边缘情况,我不建议使用正则表达式来搜索JSON字符串。但是,由于听起来您对JSON字符串有创造性的控制权,因此您应该能够避免许多会导致问题的边缘情况。
此正则表达式将执行以下操作:
label
并返回其值\"
个子字符串,因此这个正则表达式在有或没有\
的正则表达式:
\{(?:"[^"]*"|[^{}"]*|\{[^{}]*})*?"label\\?":\K"([^"]*)\\?"
来源文字
.....{\"label\":\"Pass Type\",\"value\":\"prof_3Day\",\"identifier\":\"field102\",\"type\":\"dropdown\",\"page\":3,\"page_name\":\"Step 3\",\"options\":[{......
生成的捕获组
label
和值集
样本匹配
[0] => \"Pass Type\"
[1] => Pass Type
\K
,因此您可以删除它。如果它被删除,那么捕获组0将包含直到label
keyname <强>详情
NODE EXPLANATION
----------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the least amount possible)):
----------------------------------------------------------------------
[,:] any character of: ',', ':'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[^{}"]* any character except: '{', '}', '"' (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\{ '{'
----------------------------------------------------------------------
[^{}]* any character except: '{', '}' (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
} '}'
----------------------------------------------------------------------
)*? end of grouping
----------------------------------------------------------------------
\K 'K'
----------------------------------------------------------------------
\\? '\' (optional (matching the most amount
possible))
----------------------------------------------------------------------
"label '"label'
----------------------------------------------------------------------
\\? '\' (optional (matching the most amount
possible))
----------------------------------------------------------------------
": '":'
----------------------------------------------------------------------
\\? '\' (optional (matching the most amount
possible))
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^"]*? any character except: '"' (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\\? '\' (optional (matching the most amount
possible))
----------------------------------------------------------------------
" '"'