REGEX-获取逗号分隔的列表允许逗号之前 /之后的空间
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])
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
regex demo 。 详细信息:
(?i)
-上的案例不敏感模式(? ?= \ s?)
- 上一个正则匹配的末端和两端的单个可选空间的逗号,或#images
string和a= char在两端上都带有单个可选的空格
(\ w+(?: - \ w+)*)
- 组1:一个或多个单词chars,然后以的零或更多重复
-
和一个或多个字符。请参阅a groovy demo :
output:
an 替代性凹槽代码:
You can use
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:
Output:
An alternative Groovy code: