我写了一个正则表达式,将文件路径解析为不同的组(DRIVE,DIR,FILE,EXTENSION)。正则表达式匹配需要很长时间才能执行
^((?<DRIVE>[a-zA-Z]):\\)*((?<DIR>[a-zA-Z0-9_]+(([a-zA-Z0-9_\s_\-\.]*[a-zA-Z0-9_]+)|([a-zA-Z0-9_]+)))\\)*(?<FILE>([a-zA-Z0-9_]+(([a-zA-Z0-9_\s_\-\.]*[a-zA-Z0-9_]+)|([a-zA-Z0-9_]+))\.(?<EXTENSION>[a-zA-Z0-9]{1,6})$))
我在C#中做了一个测试。当我想测试的路径是正确的。结果非常快,这就是我想要的。
string path = @"C:\Documents and Settings\jhr\My Documents\Visual Studio 2010\Projects\FileEncryptor\Dds.FileEncryptor\Dds.FileEncryptor.csproj";
=> OK
但是,当我尝试,我知道这将不匹配的路径来测试,这样的:
string path = @"C:\Documents and Settings\jhr\My Documents\Visual Studio 2010\Projects\FileEncryptor\Dds.FileEncryptor\Dds.FileEncryptor?!??????";
=> BUG
测试当我称这部分代码时冻结
Match match = s_fileRegex.Match(path);
当我查看我的Process Explorer时,发现处理器的100%挂起了进程QTAgent32.exe。这是什么意思 ?
您在验证之前是否考虑过[解析路径](http://stackoverflow.com/questions/3386258/parsing-a-given-path-in-c)? – kennytm 2010-08-04 07:44:08
为什么不在Path类中使用有用的方法?没有必要使用正则表达式来提取这些信息。 – Jens 2010-08-04 07:46:28
RegEx真的是走错路。我仍然想知道为什么这RegEx杀死regexr.com:> – atamanroman 2010-08-04 07:48:02