Ruby正则表达式过滤掉带有“字符串”后缀的单词结尾

时间:2017-11-08 19:42:57

标签: ruby regex gitlab

我正在尝试提供一个与以下字符串匹配的Ruby Regex:

MAINT: Refactor something
STRY-1: Add something
STRY-2: Update something

但不应与以下内容相符:

MAINT: Refactored something
STRY-1: Added something
STRY-2: Updated something

MAINT: Refactoring something
STRY-3: Adding something
STRY-4: Updating something

基本上,:之后的第一个字不应该以{{1​​}}或ed

结尾

这就是我目前的情况:

ing

我已尝试^(MAINT|(STRY|PRB)-\d+):\s([A-Z][a-z]+)\s([a-zA-Z0-9._\-].*) [^ed],但由于我的目标不仅仅是单个字符,因此无法在此处使用。

我无法找到合适的解决方案来实现这一目标。

3 个答案:

答案 0 :(得分:6)

您可以使用

^[-\w]+:\s*(?:(?!(?:ed|ing)\b)\w)+\b.+

a demo on regex101.com

<小时/> 细分说明:

^                     # start of the line/string
[-\w]+:\s*            # match - and word characters, 1+ then :
(?:                   # non-capturing group
    (?!(?:ed|ing)\b)  # neg. lookahead: no ed or ing followed by a word boundary
    \w                # match a word character
)+\b                  # as long as possible, followed by a boundary
.*                    # match the rest of the string, if any

<小时/> 我没有Ruby的经验,但我想您可以选择拆分并检查第二个单词是以ed还是ing结尾。对于未来的程序员/同事来说,后一种方法可能更容易处理。

答案 1 :(得分:2)

r = /
    \A             # match beginning of string
    (?:            # begin a non-capture group
      MAINT        # match 'MAINT'
      |            # or
      STRY\-\d+    # match 'STRY-' followed by one or more digits
    )              # end non-capture group
    :[ ]           # match a colon followed by a space
    [[:alpha:]]+   # match one or more letters
    (?<!           # begin a negative lookbehind
      ed           # match 'ed'
      |            # or
      ing          # match 'ing'
    )              # end negative lookbehind
    [ ]            # match a space
    /x             # free-spacing regex definition mode

   "MAINT: Refactor something".match?(r)   #=> true
   "STRY-1: Add something".match?(r)       #=> true
   "STRY-2: Update something".match?(r)    #=> true

   "MAINT: Refactored something".match?(r) #=> false
   "STRY-1: Added something".match?(r)     #=> false
   "STRY-2: Updated something".match?(r)   #=> false

   "A MAINT: Refactor something".match?(r) #=> false
   "STRY-1A: Add something".match?(r)      #=> false

此正则表达式通常按如下方式编写。

r = /\A(?:MAINT|STRY\-\d+): [[:alpha:]]+(?<!ed|ing) /

这样表达的两个空格都可以用空格字符表示。但是,在自由间隔模式下,删除了字符类之外的所有空格,这就是我需要将每个空格括在字符类中的原因。

答案 2 :(得分:0)

(代表作者提问)

这是我最终使用的:

ORACLE_HOME