简体   繁体   中英

Regex or Substring to Match Filename With Extension

I have a current situation where I can be given a filename with path that looks like:

C:\\Users\\testUser\\Documents\\testFile.txt.9043632d298f44ad88509c677a8249f8

or

C:\\Users\\testUser\\Documents\\testFile.txt.9043632d298f44ad88509c677a8249f8.enc

I need to be able to extract everything up until the end of the extension (can be any file extension, will always have guid string preceded by a. after the extension)

So an example output would be:

C:\\Users\\testUser\\Documents\\testFile.txt
C:\\Users\\testUser\\Documents\\testFile.pdf
C:\\Users\\testUser\\Documents\\testFile.jpeg

I have tried substrings but cannot seem to get the proper input (though I assume it is a simple task). I can never seem to get the proper result.

An example I tried was along the lines of:

filename.Substring(0,filename.Indexof('.', //what goes here??));

but keep getting stuck.

Any help would be lovely!

You might use:

new Regex(@".*(?=\.[a-f\d]{32})", RegexOptions.IgnoreCase).Match(yourString)

Explanation :

.+ match one or more of any char

(?= ) look ahead, check if the following chars match, but don't include in match

\. match a dot

[af\d]{32} match any character af or digit exactly 32 times

RegexOptions.IgnoreCase ignores the case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM