Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Removing file extension using REGEX

807605Aug 28 2007 — edited Aug 28 2007
                // User didn't add the XLS extension; add it automatically
                if (file.getName ().lastIndexOf (".") > 0)
                {
                    fileName = file.getName ().toLowerCase ().replaceAll (".*$", ".xls");
                    System.out.println ("*** ALREADY HAS AN EXTENSION ***");
                    System.out.println ("Renamed to " + fileName);
                }
                ...
What I'm trying to do is remove a file extension from a string and add my own. For example, in my application, I'm checking to see if there is a period in the file.getName() string. If there is, I want to replace anything from the period on with nothing, but I don't think my regular expression is right.

If I type in "file.txt", I get:
*** ALREADY HAS AN EXTENSION ***
Renamed to .xls.xls


Basically, my regular expression is incorrect, but I don't see what I'm missing.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 25 2007
Added on Aug 28 2007
14 comments
2,645 views