Getting rid of multiple periods in a file name with RegEx

I have an application that requires me to "clean up" dirty "file names.

I was wondering if anyone knows how to handle files named like this:

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

In principle, there is no guarantee that the periods will be in one place for each file name. I was hoping to get back through the drive, look for periods in the file name itself (minus the extension), delete the period, and then add the extension to it.

Does anyone know how best to do this or how to accomplish what I hope to do? As a side note, regEx is REQUIRED for this project.

EDIT: instead of viewing 1.0.1.21 - Confidential ... doc, I would like: 10121 - Confidential.doc

For a different file name instead of Accounting.Files.doc, I would like to see AccountingFiles.doc

+1
source share
3 answers

You can do this with a regex:

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: delete every dot that has at least one dot after it.

It would be easier to use built-in methods to handle file names and extensions, so if you could somehow remove the requirement that it should be regular expressions, I think this will make the solution even better.

+5
source

Here is an alternative solution that does not use regular expressions - perhaps this is more readable:

 string s = "1.0.1.21 -- Confidential...doc"; int extensionPoint = s.LastIndexOf("."); if (extensionPoint < 0) { extensionPoint = s.Length; } string nameWithoutDots = s.Substring(0, extensionPoint).Replace(".", ""); string extension = s.Substring(extensionPoint); Console.WriteLine(nameWithoutDots + extension); 
+2
source

I would do it without regular expressions *. (Disclaimer: I am not very good at regular expressions, so maybe that's why.)

Consider this option.

 string RemovePeriodsFromFilename(string fullPath) { string dir = Path.GetDirectoryName(fullPath); string filename = Path.GetFileNameWithoutExtension(fullPath); string sanitized = filename.Replace(".", string.Empty); string ext = Path.GetExtension(fullPath); return Path.Combine(dir, sanitized + ext); } 

* Whoops . It sounds like you said using regular expressions is a requirement. Never mind! (Although I have to ask: why?)

+2
source

Source: https://habr.com/ru/post/1316316/


All Articles