The initial string is [image:salmon-v5-09-14-2011.jpg]
初始字符串为[image:salmon-v5-09-14-2011.jpg]
I would like to capture the text "salmon-v5-09-14-2011.jpg" and used GSkinner's RegEx Tool
我想捕获的文本“鲑-v5-09-14-2011 - jpg”和使用GSkinner的RegEx工具
The closest I can get to my desired output is using this RegEx:
最接近我想要的输出的是使用这个RegEx:
:([\w+.-]+)
The problem is that this sequence includes the colon and the output becomes
问题是这个序列包含冒号,输出变成
:salmon-v5-09-14-2011.jpg
How can I capture the desired output without the colon. Thanks for the help!
如何不用冒号捕获所需的输出。谢谢你的帮助!
3 个解决方案
#1
22
Use a look-behind:
使用一个向后看:
(?<=:)[\w+.-]+
A look-behind (coded as (?<=someregex)
) is a zero-width match, so it asserts, but does not capture, the match.
look-behind(编码为(?<=someregex)是一个零宽度的匹配,所以它断言,但不捕获匹配。
Also, your regex may be able to be simplified to this:
此外,您的regex可能可以简化为以下内容:
(?<=:)[^\]]+
which simply grabs anything between (but not including) a :
and a ]
它只抓取(但不包括)a:和a)之间的任何东西
#2
4
If you are always looking at strings in that format, I would use this pattern:
如果你总是看那种格式的字符串,我会用这种模式:
(?<=\[image:)[^\]]+
This looks behind for [image:
, then matches until the closing ]
它在后面查找[image:,然后匹配直到结束]
#3
1
You have the correct regex only the tool you're using is highlighting the entire match and not just your capture group. Hover over the match and see what "group 1" actually is.
您有正确的regex,只有您使用的工具是突出显示整个匹配,而不仅仅是您的捕获组。将鼠标悬停在比赛上,看看“第一组”到底是什么。
If you want a slightly more robust regex you could try :([^\]]+)
which will allow for any characters other than ]
to appear in the file name portion.
如果你想要一个更健壮的正则表达式可以尝试:([^ \]]+)将允许以外的任何字符)出现在文件名部分。
#1
22
Use a look-behind:
使用一个向后看:
(?<=:)[\w+.-]+
A look-behind (coded as (?<=someregex)
) is a zero-width match, so it asserts, but does not capture, the match.
look-behind(编码为(?<=someregex)是一个零宽度的匹配,所以它断言,但不捕获匹配。
Also, your regex may be able to be simplified to this:
此外,您的regex可能可以简化为以下内容:
(?<=:)[^\]]+
which simply grabs anything between (but not including) a :
and a ]
它只抓取(但不包括)a:和a)之间的任何东西
#2
4
If you are always looking at strings in that format, I would use this pattern:
如果你总是看那种格式的字符串,我会用这种模式:
(?<=\[image:)[^\]]+
This looks behind for [image:
, then matches until the closing ]
它在后面查找[image:,然后匹配直到结束]
#3
1
You have the correct regex only the tool you're using is highlighting the entire match and not just your capture group. Hover over the match and see what "group 1" actually is.
您有正确的regex,只有您使用的工具是突出显示整个匹配,而不仅仅是您的捕获组。将鼠标悬停在比赛上,看看“第一组”到底是什么。
If you want a slightly more robust regex you could try :([^\]]+)
which will allow for any characters other than ]
to appear in the file name portion.
如果你想要一个更健壮的正则表达式可以尝试:([^ \]]+)将允许以外的任何字符)出现在文件名部分。