【问题标题】:Regex issue with finding words partially部分查找单词的正则表达式问题
【发布时间】:2017-01-19 06:41:21
【问题描述】:

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

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 它具有其余重要的示例字符串。

【问题讨论】:

  • 所以你想匹配if (typeof typeaheadResult !== "undefined") 部分?因为您的正则表达式匹配以typeaheadResult({ 开头的内容,所以无法匹配整个字符串。
  • 当你对上面的字符串说“它不起作用”时,你的意思是它不匹配,还是它匹配但在匹配的组中返回了一些意想不到的东西?
  • 我会尝试typeaheadResult\({"Q":("[\w\s]*"),"R":\[+("[\w\s]*"|[^]]*) 匹配R 之后的双引号字符串或] 以外的任何0+ 字符。
  • @WiktorStribiżew 它说character class has ']' without escape
  • 只需添加转义符[^\]]*

标签: ruby regex rubular


【解决方案1】:

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

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

更新

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

【讨论】:

  • 你的回答比我快 30 秒 :)
  • 什么意思?它已经在R:[ \w... 之后有了加号,因为这个词可能是一个或多个字母.. 看到这个:"R\":\[+("\w+\s?\w+\s?\w+\s?\w+")
  • 我的意思是你的regex 指定R:[ 部分之后的引号之间必须有一些东西。您示例中的字符串不包含它,因此它不匹配。如果在) 之后添加?,则整个组变为可选,并且字符串将匹配。
  • 我说必须至少有 4 个字符,因为有 4 个 \w+ 部分。 abcd 会匹配(每个字母都会匹配\w+),但abc 不会匹配(仍然会有一个不匹配的\w+)。
【解决方案2】:

我假设每个段落最多有一个匹配项,并且对于给定段落中的匹配项,"[]" 跟在 "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 不是nil 并且$1.begins_with($2)false,您可能会考虑标记可能的错误数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2022-08-18
    • 2016-12-07
    • 2015-09-03
    相关资源
    最近更新 更多