Ruby regex to filter a word ending with the suffix "string"

I am trying to find a Ruby Regex that will match the following line:

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

But it should not match the following:

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

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

Basically, the first word after :should not end either ed, oring

This is what I have:

^(MAINT|(STRY|PRB)-\d+):\s([A-Z][a-z]+)\s([a-zA-Z0-9._\-].*)

I tried [^ed]and [^ing], but they won’t work here, as I am targeting more than one character.

I cannot find a suitable solution to achieve this.

+4
source share
3 answers

you can use

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

Watch the demo at regex101.com .


In a word, it says:
^                     # 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. /.
+6
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

(Sent on behalf of the author of the question).

Here is what I ended up using:

^(MAINT|(STRY|PRB)-\d+):\s(?:(?!(?:ed|ing)\b)[A-Za-z])+\s([a-zA-Z0-9._\-].*)
0
source

Source: https://habr.com/ru/post/1688960/


All Articles