Regex in C# that contains “this” but not "that

和自甴很熟 提交于 2019-12-11 12:49:51

问题


I need to filter a list of files. Some of these are csv files, and of those, some of them are appended with a control tag, ".cntl".

Example: file1.csv, file1.csv.cntl

I would like to set up a regex that checks to see if a file contains "csv" and NOT "cntl". Right now I've got this.

csv(?!cntl)

This is not working. What would a proper regex be?

PS. This is all done in C#.


回答1:


Your regex should check to see if a file ends with .csv

\.csv$

Remember that csv could be contained elsewhere in the file name.




回答2:


The following is the pattern you want.

@"csv(?!\.cntl)"

But, wouldn't it be easier to check:

if (string.EndsWith("cntl"))

Using a regex is unnecessarily complicated.




回答3:


This should work:

csv(?!.*cntl)


来源:https://stackoverflow.com/questions/4402811/regex-in-c-sharp-that-contains-this-but-not-that

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