我假设每个段落最多有一个匹配项,并且对于给定段落中的匹配项,要么"[]"
紧随其后,要么紧随"R:
其后并以下"Q":"
一个双引号之前的字符结尾的字符串以紧随其后的字符串开头"R":[["
并以下一个双引号之前的字符。
str =<<BITTER_END
\if (typeof typeaheadResult !== "undefined") { typeaheadResult({"Q":"standing desk Mike","R":[["standing desk",[["Home",4044]]],"standing desk converter","adjustable standing desk","standing desk 48","tabletop standing desk","glass standing desk desk"]}) }
if (typeof typeaheadResult !== "undefined") { typeaheadResult({"Q":"laptop bag","R":[["laptop bag",[["Electronics",3944]]],"laptop bags for 15.6 inch laptops","laptop bags for women","laptop bag 15.6","laptop bags for 17.3 in laptops","rolling laptop bag","laptop bag with wheels","laptop bag 17\""]}) }
\if (typeof typeaheadResult !== "undefined") { typeaheadResult({"Q":"sitting desk Melba","R":[["standing desk",[["Home",4044]]],"standing desk converter","adjustable standing desk","standing desk 48","tabletop standing desk","glass standing desk desk"]}) }
if (typeof typeaheadResult !== "undefined") { typeaheadResult({"Q":"crapshoot hello","R":[]}) }
BITTER_END
r = /
typeaheadResult\(\{\"Q\":\" # match 'typeaheadResult({"Q":"'
([[[:alnum:]]\s]+) # match letters, digits and spaces in capture group 1
\",\"R\": # match string
(?: # begin non-capture group
\[\[\" # match 2 left brackets and a double quote
([[[:alnum:]]\s]+) # match > 0 letters, digits and spaces in capture group 2
| # or
(\[\]) # match left then right bracket in capture group 3
) # end non-capture group
/x # free-spacing regex definition mode
str.split(/\n\n+/).map do |s|
ss = s[r]
ss = nil unless (($2 && $1 =~ /\A#{$2}/) || $3=="[]")
ss
end.compact
#=> ["typeaheadResult({\"Q\":\"standing desk Mike\",\"R\":[[\"standing desk",
# "typeaheadResult({\"Q\":\"laptop bag\",\"R\":[[\"laptop bag",
# "typeaheadResult({\"Q\":\"crapshoot hello\",\"R\":[]"]
如果$2
is notnil
和$1.begins_with($2)
is ,您可能会考虑标记可能的不良数据false
。