正则表达式匹配_或字符串结尾 [英] Regex to match _ or end of string
问题描述
我正在使用MATLAB的regexp()
,并且试图找到一个仅与包含Cyto
而不与CytoBlue
的文件名匹配的正则表达式.我的问题是文件名看起来像Texture_Variance_Cyto_4_90
和Texture_Variance_CytoBlue_4_90
或HIST_9BinsHistBin7_Cyto
和HIST_9BinsHistBin7_CytoBlue
.
I'm working with MATLAB's regexp()
and I'm trying to find a regular expression that would match only file names containing Cyto
but not CytoBlue
. My problem is that the file names look either like Texture_Variance_Cyto_4_90
and Texture_Variance_CytoBlue_4_90
, or HIST_9BinsHistBin7_Cyto
and HIST_9BinsHistBin7_CytoBlue
.
如果我只是尝试匹配Cyto
,我还将捕获所有包含CytoBlue
的文件.如果尝试匹配Cyto_
,则会错过文件名,其中Cyto
是最后一个元素.我想我需要说匹配_或字符串的末尾"的内容.我尝试了Cyto[_\Z]
,但是那行不通,我再次错过了所有以Cyto
结尾的元素.
If I just try to match Cyto
, I also capture all the files containing CytoBlue
. If I try to match Cyto_
, I miss the file names where Cyto
is the last element. I guess I'd need something that says "match either _ or the end of the string". I tried Cyto[_\Z]
but that does not work, I again miss all the elements that ends with Cyto
.
推荐答案
Cyto(?=$|_)
此匹配Cyto
,后跟("(?=...)"
)字符串("$"
)或_
的末尾.请注意,下划线不会作为匹配的一部分返回.
This matches Cyto
, followed by ("(?=...)"
) the end of the string ("$"
) or _
. Note that the underscore is not returned as part of the match.
这篇关于正则表达式匹配_或字符串结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!