REGEX-获取逗号分隔的列表允许逗号之前 /之后的空间

发布于 2025-01-19 11:55:52 字数 902 浏览 0 评论 0 原文

I try to extract an images array/list from a commit message:

String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"

I want to get a list that contains:

["image-a", "image-b", "image_c", "imaged", "image-e"]

NOTES:

A) should allow a single space before/after the comma (,)

B) ensure that #images = exists but exclude it from the group

C) I also searching for other parameters like #build and #setup so I need to ignore them when looking for #images

What I have until now is:

/(?i)#images\s?=\s?<HERE IS THE MISSING LOGIC>/

我使用Find()方法:

def matcher = commitMsg =~ /(?i)#images\s?=\s?([^,]+)/ 

if(matcher.find()){
    println(matcher[0][1])
}

I try to extract an images array/list from a commit message:

String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"

I want to get a list that contains:

["image-a", "image-b", "image_c", "imaged", "image-e"]

NOTES:

A) should allow a single space before/after the comma (,)

B) ensure that #images = exists but exclude it from the group

C) I also searching for other parameters like #build and #setup so I need to ignore them when looking for #images

What I have until now is:

/(?i)#images\s?=\s?<HERE IS THE MISSING LOGIC>/

I use find() method:

def matcher = commitMsg =~ /(?i)#images\s?=\s?([^,]+)/ 

if(matcher.find()){
    println(matcher[0][1])
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2025-01-26 11:55:52

您可以使用

(?i)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)

regex demo 详细信息

  • (?i) -
  • 上的案例不敏感模式(? ?= \ s?) - 上一个正则匹配的末端和两端的单个可选空间的逗号,或 #images string和a = char在两端上都带有单个可选的空格
  • (\ w+(?: - \ w+)*) - 组1:一个或多个单词chars,然后以的零或更多重复 - 和一个或多个字符。

请参阅a groovy demo

String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"
def re = /(?i)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)/
def res = (commitMsg =~ re).collect { it[1] }
print(res)

output:

[image-a, image-b, image_c, imaged, image-e]

an 替代性凹槽代码

String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"
def re = /(?i)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)/
def matcher = (commitMsg =~ re).collect()
for(m in matcher) {
    println(m[1])
}

You can use

(?i)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)

See the regex demo. Details:

  • (?i) - case insensitive mode on
  • (?:\G(?!^)\s?,\s?|#images\s?=\s?) - either the end of the previous regex match and a comma enclosed with single optional whitespaces on both ends, or #images string and a = char enclosed with single optional whitespaces on both ends
  • (\w+(?:-\w+)*) - Group 1: one or more word chars followed with zero or more repetitions of - and one or more word chars.

See a Groovy demo:

String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"
def re = /(?i)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)/
def res = (commitMsg =~ re).collect { it[1] }
print(res)

Output:

[image-a, image-b, image_c, imaged, image-e]

An alternative Groovy code:

String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"
def re = /(?i)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)/
def matcher = (commitMsg =~ re).collect()
for(m in matcher) {
    println(m[1])
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文