正则表达式 - 获取逗号分隔的列表允许在逗号之前/之后有空格
Regex - get list comma separated allow spaces before / after the comma
我尝试从提交消息中提取图像 array/list:
String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"
我想获取包含以下内容的列表:
["image-a", "image-b", "image_c", "imaged", "image-e"]
备注:
A)应该允许一个单spacebefore/after逗号(,)
B) 确保 #images =
存在但将其排除在组
之外
C) 我还搜索其他参数,例如#build 和#setup,因此在查找#images 时我需要忽略它们
到目前为止我得到的是:
/(?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)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)
见regex demo。 详情:
(?i)
- 不区分大小写模式开启
(?:\G(?!^)\s?,\s?|#images\s?=\s?)
- 前一个正则表达式匹配的结尾和两端用单个可选空格括起来的逗号,或者 #images
字符串和用单个可选空格括起来的 =
字符两端的空格
(\w+(?:-\w+)*)
- 第 1 组:一个或多个单词字符后跟零次或多次重复的 -
和一个或多个单词字符。
看到一个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)
输出:
[image-a, image-b, image_c, imaged, image-e]
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])
}
我尝试从提交消息中提取图像 array/list:
String commitMsg = "#build #images = image-a, image-b,image_c, imaged , image-e #setup=my-setup fixing issue with px"
我想获取包含以下内容的列表:
["image-a", "image-b", "image_c", "imaged", "image-e"]
备注:
A)应该允许一个单spacebefore/after逗号(,)
B) 确保 #images =
存在但将其排除在组
C) 我还搜索其他参数,例如#build 和#setup,因此在查找#images 时我需要忽略它们
到目前为止我得到的是:
/(?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)(?:\G(?!^)\s?,\s?|#images\s?=\s?)(\w+(?:-\w+)*)
见regex demo。 详情:
(?i)
- 不区分大小写模式开启(?:\G(?!^)\s?,\s?|#images\s?=\s?)
- 前一个正则表达式匹配的结尾和两端用单个可选空格括起来的逗号,或者#images
字符串和用单个可选空格括起来的=
字符两端的空格(\w+(?:-\w+)*)
- 第 1 组:一个或多个单词字符后跟零次或多次重复的-
和一个或多个单词字符。
看到一个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)
输出:
[image-a, image-b, image_c, imaged, image-e]
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])
}