none
Regex, remove file extensions PLZ HELP !

    Question

  • Hi !

     

    Does anybody know how do I can remove all extensions of file with regex rules ???

     

    for example file: sdfkghsjhg3248-sdf_034--d3.org.filt.dat

    should be: sdfkghsjhg3248-sdf_034--d3

     

    Right now I'm using quick fix:

    ^((\w*-*)+)

    But it is unlikely going to work with all possible filenames.

     

    PLZ HELP !

     

    Thursday, September 07, 2006 6:49 AM

Answers

All replies

  • Path.GetFileNameWithoutExtension Method does the same.
    Thursday, September 07, 2006 6:57 AM
  • Yes but it removes only last one extension, for examlpe file test.txt.txt will return test.txt :-(
    Monday, September 18, 2006 6:53 AM
  • how about running it again and again until all extension are gone :)
    Monday, September 18, 2006 8:37 AM
  • Hey there,

    I have looked into your problem and there is actually no way to really get just hte file name without the extension. All you can do is either remove everything and including the last dot in the filename or just take the eveything before the first dot in the filename ( which would be [\w-]+)

    Reason being is that filenames alone may contain dots, which is possible
    i can call my file my.txt and it would have the extension txt reaulting into the full name my.txt.txt

    Another option would be to create a source of all valid file extensions (maybe to get them from the system) to get the filename only, which is in this case a pretty darn work, and also not accurate since there are almost infinite file extensions.

    Hope I could clear up the matter a bit.

    Monday, September 18, 2006 9:01 AM
  • Thanks !

    first match of: [\w-]+

    should do it.

    I consider everything beside first dot is extension

     

    Monday, September 18, 2006 11:48 AM