首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用REGEX在使用Ruby的范围内找到一组独特的单词?

如何使用REGEX在使用Ruby的范围内找到一组独特的单词?
EN

Stack Overflow用户
提问于 2013-12-20 12:47:46
回答 1查看 444关注 0票数 0

我希望创建一个符合以下要求的Regex:

( 1)必须充当“和”陈述

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

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

到目前为止,我有这个工作REGEX,它满足1和2。

代码语言:javascript
复制
/(word1|word2)(?:\W+\w+){0,3}?\W+(word1|word2)/i

示例Regex:

/(cat|dog)(?:\W+\w+){0,3}?\W+(cat|dog)/i

现在可以工作的字符串

  • 那只猫吓到了另一只猫。
  • 猫喜欢狗。
  • 狗喜欢猫。
  • 狗讨厌狗。

我不想要的字符串

  • 那只猫吓到了另一只猫。
  • 狗讨厌狗。

像“猫吓到了另一只猫”这样的短语。将与REGEX匹配,因为它正在搜索第二组中的任何单词,其中包括cat。不过,我不想让它自己去寻找。我只想找狗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-20 13:21:23

不如:

代码语言:javascript
复制
/(cat|dog)(?:\W+\w+){0,3}?\W+(?!\1)(cat|dog)/

解释:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20703980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档