Regex to match _ or end of string

纵饮孤独 提交于 2019-12-23 18:19:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!