Ruby正则表达式过滤掉带有“字符串”后缀的单词结尾
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby正则表达式过滤掉带有“字符串”后缀的单词结尾相关的知识,希望对你有一定的参考价值。
我想提出一个与以下字符串匹配的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
基本上,:
之后的第一个词不应该以ed
或ing
结束
这就是我目前所拥有的:
^(MAINT|(STRY|PRB)-d+):s([A-Z][a-z]+)s([a-zA-Z0-9._-].*)
我已经尝试过[^ed]
和[^ing]
,但他们不会在这里工作,因为我的目标不仅仅是单个角色。
我无法找到适当的解决方案来实现这一目标。
答案
你可以用
^[-w]+:s*(?:(?!(?:ed|ing))w)+.+
Broken down this says:
^ # start of the line/string
[-w]+:s* # match - and word characters, 1+ then :
(?: # non-capturing group
(?!(?:ed|ing)) # neg. lookahead: no ed or ing followed by a word boundary
w # match a word character
)+ # as long as possible, followed by a boundary
.* # match the rest of the string, if any
I have no experience in
Ruby
but I guess you could alternatively do a split and check if the second word ends with ed
or ing
. The latter approach might be easier to handle for future programmers/colleagues.
另一答案
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) /
以这种方式表达,两个空格可以各自表示空格字符。但是,在自由间隔模式下,字符类之外的所有空格都被删除,这就是我需要将每个空格括在一个字符类中的原因。
另一答案
(代表作者提问)。
这是我最终使用的:
^(MAINT|(STRY|PRB)-d+):s(?:(?!(?:ed|ing))[A-Za-z])+s([a-zA-Z0-9._-].*)
以上是关于Ruby正则表达式过滤掉带有“字符串”后缀的单词结尾的主要内容,如果未能解决你的问题,请参考以下文章
用正则表达批量快速解决如“过滤注释的//”“查询特定单词”“匹配特定子字符串”等工作中难题