0

我有一个正则表达式适用于某些单词但不是全部:

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":[]}) }

我的正则表达式不适用于上述字符串,我认为这不是因为使用了不恰当的词。这是我尝试过的 rubular 永久链接:http ://rubular.com/r/WOr7xYPePs 它具有其余重要的示例字符串。

4

2 回答 2

0

后面的部分R:[必须包含至少 4 个\w字符"

如果它是可选的,则必须添加一个?.

更新

?只需在正则表达式末尾添加 a即可解决问题: http ://rubular.com/r/HUmtoffTmi

于 2016-09-11T19:52:10.987 回答
0

我假设每个段落最多有一个匹配项,并且对于给定段落中的匹配项,要么"[]"紧随其后,要么紧随"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\":[]"] 

如果$2is notnil$1.begins_with($2)is ,您可能会考虑标记可能的不良数据false

于 2016-09-12T18:33:40.090 回答