Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用正则表达式在使用Ruby的范围内查找一组唯一的单词?_Ruby_Regex_Jruby - Fatal编程技术网

如何使用正则表达式在使用Ruby的范围内查找一组唯一的单词?

如何使用正则表达式在使用Ruby的范围内查找一组唯一的单词?,ruby,regex,jruby,Ruby,Regex,Jruby,我希望创建一个符合以下要求的正则表达式: 1) 必须充当“AND”语句 2) 这两个词应该在一个范围内 3) 它不计算同一单词中的两个 到目前为止,我有一个工作正则表达式,它满足1和2 /(word1|word2)(?:\W+\w+){0,3}?\W+(word1|word2)/i 正则表达式示例: /(猫狗)(?:\W+\W+{0,3}?\W+(猫狗)/i 现在可用的字符串 那只猫吓坏了另一只猫 猫喜欢狗 狗喜欢猫 狗讨厌狗 我不想要的字符串 那只猫吓坏了另一只猫 狗讨厌狗 诸如“猫

我希望创建一个符合以下要求的正则表达式:

1) 必须充当“AND”语句

2) 这两个词应该在一个范围内

3) 它不计算同一单词中的两个

到目前为止,我有一个工作正则表达式,它满足1和2

/(word1|word2)(?:\W+\w+){0,3}?\W+(word1|word2)/i
正则表达式示例:
/(猫狗)(?:\W+\W+{0,3}?\W+(猫狗)/i

现在可用的字符串

  • 那只猫吓坏了另一只猫

  • 猫喜欢狗

  • 狗喜欢猫

  • 狗讨厌狗

我不想要的字符串

  • 那只猫吓坏了另一只猫

  • 狗讨厌狗

诸如“猫吓坏了另一只猫”之类的短语将与此正则表达式匹配,因为它正在搜索第二组中的任何单词,其中包括cat。然而,我不想让它自己寻找。我只想用它来搜索狗。

怎么样:

/(cat|dog)(?:\W+\w+){0,3}?\W+(?!\1)(cat|dog)/
说明:

The regular expression:

(?-imsx:(cat|dog)(?:\W+\w+){0,3}?\W+(?!\1)(cat|dog))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    cat                      'cat'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    dog                      'dog'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture (between 0 and 3
                           times (matching the least amount
                           possible)):
----------------------------------------------------------------------
    \W+                      non-word characters (all but a-z, A-Z,
                             0-9, _) (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  ){0,3}?                  end of grouping
----------------------------------------------------------------------
  \W+                      non-word characters (all but a-z, A-Z, 0-
                           9, _) (1 or more times (matching the most
                           amount possible))
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    \1                       what was matched by capture \1
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    cat                      'cat'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    dog                      'dog'
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

2) 它的意思是什么?单词1应该在单词2的三个单词以内。这实际上已经起作用了。我的问题是,word1除了寻找word2之外,还在寻找自己(word1)。我只想让它查找word2,而不是word1,同时保持三个单词的范围。您的要求不清楚,您可以添加几个示例字符串,每个字符串的结果都是您希望的。当然,就是这样!基本上,我试图避免用一个词搜索同一个词。它必须是正则表达式,还是使用解析器也是可以接受的?是的!这正是我想要的。我在Rubular上测试了一下。谢谢我觉得正则表达式令人反感。如果解释来自正则表达式测试人员(如Rubular),那么是哪一个@达米安,今天早上我的反应有点慢。不仅仅是朋友?@CarySwoveland:这是一个CPAN模块