在文件名中使用RegEx摆脱多个周期的 [英] Getting rid of multiple periods in a filename using RegEx

查看:99
本文介绍了在文件名中使用RegEx摆脱多个周期的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求我干净的脏文件名的应用程序。



我想知道是否有人知道如何处理被命名为喜欢的文件:



1.0.1.21 - 机密文档...

Accounting.Files.doc



基本上没有保证的期限将在每个文件相同的地方名称。我希望通过驱动递归,搜索文件名本身(去掉扩展名)时,请取下期再延长追加到它。



有谁既知道一个更好的办法做到这一点还是如何执行什么我希望做什么?
作为一个说明,正则表达式是这个项目的要求。



编辑:而不是看到1.0.1.21的 - 机密......医生,我想看看:10121 - Confidential.doc


。对于其他的文件名,而是Accounting.Files.doc的,我想看到AccountingFiles.doc


解决方案
- 机密文档...

 字符串s =1.0.1.21:

您可以用正则表达式做
S = Regex.Replace(S,@\(= * \)。?。,);
Console.WriteLine(S);



结果:

 
10121 - Confidential.doc

正则表达式可以如下细分

 
\。匹配一个点
(?=开始超前
*任意字符
\。另一点
)关闭超前

或者用简单的英语。取出所有有后至少一点点



这将是更清洁的使用建在处理文件名和扩展名,因此,如果你能以某种方式删除,它必须是正则表达式我认为这将让解决方案更加美好的要求的方法。


I have an application that requires me to "clean" "dirty" filenames.

I was wondering if anybody knew how to handle files that are named like:

1.0.1.21 -- Confidential...doc or Accounting.Files.doc

Basically there's no guarantee that the periods will be in the same place for every file name. I was hoping to recurse through a drive, search for periods in the filename itself (minus the extension), remove the period and then append the extension onto it.

Does anybody know either a better way to do this or how do perform what I'm hoping to do? As a note, regEx is a REQUIREMENT for this project.

EDIT: Instead of seeing 1.0.1.21 -- Confidential...doc, I'd like to see: 10121 -- Confidential.doc
For the other filename, Instead of Accounting.Files.doc, i'd like to see AccountingFiles.doc

解决方案

You could do it with a regular expression:

string s = "1.0.1.21 -- Confidential...doc";
s = Regex.Replace(s, @"\.(?=.*\.)", "");
Console.WriteLine(s);

Result:

10121 -- Confidential.doc

The regular expression can be broken down as follows:

\.    match a literal dot
(?=   start a lookahead 
.*    any characters
\.    another dot
)     close the lookahead

Or in plain English: remove every dot that has at least one dot after it.

It would be cleaner to use the built in methods for handling file names and extensions, so if you could somehow remove the requirement that it must be regular expressions I think it would make the solution even better.

这篇关于在文件名中使用RegEx摆脱多个周期的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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