惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - Zhongjian Zhang

.net framework profiles /.net framework 配置 人生的意义岂止是谋生-新视野大学英语第二册第八单元A课文 - Zhongjian Zhang - 博客园 格式化类型和分析字符串 类接口/Class Interface LG 3D投影 Textbox/ComboBox 自动完成AutoCompletion Bloom's Taxonomy/布鲁姆分类 Data Structure/数据结构 in .Net Custom Shapes/Combin Shapes/合并图形 in PowerPoint 2010 scale up vs scale out/Scale vertically vs. horizontally Window Types(Tool windows/document windows)/窗口类型(工具窗口/文档窗口) in VS VS2010中shortcut key快捷键一览下载 随机long(Random long/NextLong) Windows Azure Storage Explorer List System Error Codes/系统错误代码 图解500强 超级计算机 In graphics: Supercomputing superpowers FTP active mode and assive mode(ftp 主动模式/被动模式) Ftp commands and options 为VM制作可引导的操作系统ISO
directory searchPattern to regex pattern
Zhongjian Zhang · 2010-09-20 · via 博客园 - Zhongjian Zhang

当使用Directory.GetFiles(string path, string searchPattern) 获取文件列表后,需要在已存在的文件名列表找出满足同样模式的文件来比较,此时需要一个regex pattern来进行匹配查找, 当只有已存在的 searchPattern,那怎样将Directory searchPattern 转化到 regex pattern呢?

Example:

search pattern:
data2010*.dat 

result of Directory.GetFiles
data20100001.dat
data20100002.dat
data20100003.dat

existing file name list:
data20090001.dat
data20090002.dat
data20090003.dat
data20100001.dat
data20100002.dat
data20100003.dat

the regex pattern to match file name

data20100001.dat
data20100002.dat
data20100003.dat

Step:

searchPattern=data2010*.dat

escape:data2010\*\.dat

replace:data2010.\.dat

post process:^data2010.\.dat$

regexpatter:^data2010.\.dat$

SimpleCode:

           string searchPattern = "data2010*.dat";
           searchPattern = Regex.Escape(searchPattern);
           Console.WriteLine(searchPattern);
           searchPattern = searchPattern.Replace(@"\?", ".").Replace(@"\*", @".*");
           Console.WriteLine(searchPattern);
           string prefix = "^";
           string sufix = "$";
           if (false) //optional
           {
               prefix = string.Empty;
           }

           if (false) //optional
           {
               sufix = string.Empty;
           }
           searchPattern = string.Format("{0}{1}{2}", prefix, searchPattern, sufix);
           Console.WriteLine(searchPattern);

Output:

data2010*.dat
data2010\*\.dat
data2010.*\.dat
^data2010.*\.dat$

Regex.Escape功能:

通过将最少量的一组字符(\、*、+、?、|、{、[、(、)、^、$、.、# 和空白)替换为其转义码,将这些字符转义。此操作指示正则表达式引擎以字面意义而非按元字符解释这些字符。

注意:

searchPattern在Directory.GetFiles会有一些不同的表现如下,所以会得到额外的文件,此时需要对得到的文件名进行过滤,此时也可使用这种方式来过滤, 例如在replace中加入更强的限制。

escape:data2010\*\.dat

replace:data2010.\.dat

post process:^data2010.\.dat$

Directory.GetFiles(string path, string searchPattern)

说明

在 searchPattern 中使用星号通配符时(如“*.txt”),扩展名长度正好为三个字符时的匹配行为与扩展名长度多余或少于三个字符时的匹配行为不同。文件扩展名正好是三个字符的 searchPattern 将返回扩展名为三个或更多字符的文件,其中前三个字符与 searchPattern 中指定的文件扩展名匹配。文件扩展名为一个、两个或三个以上字符的 searchPattern 仅返回扩展名长度正好与 searchPattern 中指定的文件扩展名匹配的文件。使用问号通配符字符时,此方法仅返回与指定文件扩展名匹配的文件。例如,假设目录下有两个文件“file1.txt”和“file1.txtother”,使用“file?.txt”搜索模式时只返回第一个文件,而使用“file*.txt”搜索模式时会同时返回这两个文件。

说明

因为此方法仅检查同时具有 8.3 文件名格式和长文件名格式的文件,类似“*1*.txt”的搜索模式可能会返回意外的文件名。例如,使用“*1*.txt”的搜索模式将返回“longfilename.txt”,因为等效的 8.3 文件名格式为“LONGFI~1.TXT”。

Reference:

Regex.Escape:

http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.escape.aspx

Directory.GetFiles

http://msdn.microsoft.com/zh-cn/library/wz42302f.aspx