问题
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
.
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
.
回答1:
Cyto(?=$|_)
This matches Cyto
, followed by ("(?=...)"
) the end of the string ("$"
) or _
. Note that the underscore is not returned as part of the match.
回答2:
use this regex: Cyto(_.*?(?= ))?\b
回答3:
MATLAB supports positive and negative lookaheads, so this this should work:
Cytp(?!Blue)
...meaning "Cyto" not followed by "Blue".
来源:https://stackoverflow.com/questions/11810229/regex-to-match-or-end-of-string