我有一些格式如下的字符串:
"text that comes before\"start\":\"Desired Info\"text that comes after"
我只想提取“所需信息”。它总是在前面,"\"start\":"
并且它只会在字符串中出现一次。我可以使用什么正则表达式来做到这一点?
This shall work:
s = "text that comes before\"start\":\"Desired Info\"text that comes after"
s[/(?<="start":")[^"]*(?=")/]
# => "Desired Info"
这里:是正则表达式:
"start":"(.*)"
在代码中:
match = /"start":(.*)"/.match("text that comes before\"start\":\"Desired Info\"text that comes after");
if match
print match[1]
end
最简单的是:
s[/"start":"(.*?)"/, 1]
#=> "Desired Info"
(?:\\"start\\":\\")(.+)(?:\\")
“所需信息”进入 NON IGNORED 捕获组。