部分查找单词的正则表达式问题
Regex issue with finding words partially
我有一个正则表达式,它对某些单词有效,但对所有单词无效:
str.scan(/typeaheadResult\(\{\"Q\":("\w+\s?\w+\s?\w+\s?\w+"),\"R\":\[+("\w+\s?\w+\s?\w+\s?\w+")/)
下面的字符串似乎没有被捕获:
if (typeof typeaheadResult !== "undefined") { typeaheadResult({"Q":"crapshoot","R":[]}) }
我的正则表达式不适用于上述字符串,我认为这不是因为使用了不恰当的词。
这是我尝试过的永久链接:http://rubular.com/r/WOr7xYPePs
它具有其余重要的示例字符串。
R:[
之后的部分必须在 "
.
之间至少包含 4 \w
个字符
如果它是可选的,则必须添加 ?
。
更新
只需在正则表达式末尾添加 ?
即可解决问题:http://rubular.com/r/HUmtoffTmi
我假设每个段落最多有一个匹配项,并且要在给定的段落中有一个匹配项,"[]"
跟在 "R:
之后或者 [=14= 之后的字符串] 并以下一个双引号之前的字符结尾,以 "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 (( && =~ /\A#{}/) || =="[]")
ss
end.compact
#=> ["typeaheadResult({\"Q\":\"standing desk Mike\",\"R\":[[\"standing desk",
# "typeaheadResult({\"Q\":\"laptop bag\",\"R\":[[\"laptop bag",
# "typeaheadResult({\"Q\":\"crapshoot hello\",\"R\":[]"]
如果 </code> 不是 <code>nil
并且 .begins_with()
是 false
,您可以考虑标记可能的错误数据。
我有一个正则表达式,它对某些单词有效,但对所有单词无效:
str.scan(/typeaheadResult\(\{\"Q\":("\w+\s?\w+\s?\w+\s?\w+"),\"R\":\[+("\w+\s?\w+\s?\w+\s?\w+")/)
下面的字符串似乎没有被捕获:
if (typeof typeaheadResult !== "undefined") { typeaheadResult({"Q":"crapshoot","R":[]}) }
我的正则表达式不适用于上述字符串,我认为这不是因为使用了不恰当的词。 这是我尝试过的永久链接:http://rubular.com/r/WOr7xYPePs 它具有其余重要的示例字符串。
R:[
之后的部分必须在 "
.
\w
个字符
如果它是可选的,则必须添加 ?
。
更新
只需在正则表达式末尾添加 ?
即可解决问题:http://rubular.com/r/HUmtoffTmi
我假设每个段落最多有一个匹配项,并且要在给定的段落中有一个匹配项,"[]"
跟在 "R:
之后或者 [=14= 之后的字符串] 并以下一个双引号之前的字符结尾,以 "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 (( && =~ /\A#{}/) || =="[]")
ss
end.compact
#=> ["typeaheadResult({\"Q\":\"standing desk Mike\",\"R\":[[\"standing desk",
# "typeaheadResult({\"Q\":\"laptop bag\",\"R\":[[\"laptop bag",
# "typeaheadResult({\"Q\":\"crapshoot hello\",\"R\":[]"]
如果 </code> 不是 <code>nil
并且 .begins_with()
是 false
,您可以考虑标记可能的错误数据。