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

私の正規表現は上記の文字列では機能しません。不適切な単語が使用されているためではないと思います。ここに私が試したものへのルーブルパーマリンクがあります: http://rubular.com/r/WOr7xYPePs 重要な残りのサンプル文字列があります。

4

2 に答える 2

0

段落ごとに最大で 1 つの一致があると想定しています。特定の段落で一致するためには、次の文字列が続くか、次の二重引用符の前の文字で終わる文字列が続くか"[]"、次の二重引用符で終わる文字列で始まります。次の二重引用符の前の文字。"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 に答える