正则表达式匹配_或字符串结尾 [英] Regex to match _ or end of string

查看:569
本文介绍了正则表达式匹配_或字符串结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MATLAB的regexp(),并且试图找到一个仅与包含Cyto而不与CytoBlue的文件名匹配的正则表达式.我的问题是文件名看起来像Texture_Variance_Cyto_4_90Texture_Variance_CytoBlue_4_90HIST_9BinsHistBin7_CytoHIST_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆