Regex for identifying the file name with multiple dots/periods- Please Help!

Hi all,

I have a functionality to extract the file name from a path.

I am currently using the regex: ^.+\([^.]+).[^.]+$ to extract the file name. It is working fine when the file name has no dot (’.’ or period).

Eg 1: Data\Downloads\Taxation.png
output of Group(1)= Taxation

But when the file name contains multiple dots the above regex is failing:

Eg 2: Data\Downloads\Taxation.1.png.

The expected outcome was Taxation.1, but the current regex is failing.

Please note that there is no upper limit on the number of dots that can be present within the file name.

Would be a great help if some one can share a regex, that could work when there is zero dots or multiple dots present in a file name.

Thank you in advance.

1 Like

Hi, where those file names comes from? File class already can give you that without need of regex… https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getfilenamewithoutextension?view=netframework-4.8

[\w,\s-]+.[A-Za-z]{3}
try this and let me know @VJ33

Hi
If this is the filepath stored in a variable named str_input
Then we can directly use Path.GetFileNameWithoutExtension method to get the Filename alone like this

str_filename = Path.GetFileNameWithoutExtension(str_input.ToString)

But I wonder how Filename has dots in it
Usually it won’t allow to have dots except before file extension
Cheers @VJ33

1 Like

Thank you all for the kind help.

@bcorrea @Palaniyappan: The files are scanned images shared by end users to an inbox which will be downloaded to a folder by the bot. Unfortunately there are no rules set for the naming of the files.

@Palaniyappan:You have helped me earlier also.Appreciate your guidance and willingness to help.I have learned a lot going through your responses in this forum. It really matters for a newbie like me.

Both @Palaniyappan& @bcorrea gave the correct solution. Since forum does not allow to mark both answers as solution and as @bcorrea gave the solution first marking its as solution.

Thank you all again.

2 Likes

Thanks for your kind words
Cheers @VJ33

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.