Results 1 to 2 of 2
  1. #1
    gapper is offline Member
    Join Date
    Jan 2008
    Posts
    17
    Rep Power
    0

    Default Regex for file extension

    Hi guys,

    I have file paths like:

    C:\dir\filename.ext

    I want to get the extension part form it. I tried the following but failed:

    Java Code:
    .[A-Za-z]*
    Please advice. I would appreciate an early response to this.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Regex rules!

    Hello

    Sorry for the delay. I had to study this topic before I could help you. Using Regex is a bit more complicated than your proposal. I created a program that uses the Regex package to do as you asked.
    Java Code:
    package p1;
    
    import java.util.regex.*;
    
    public class Main {
        public static void main(String[] args) {
            /*
            Regex: (?<=\\.).*$
            1.)     (?<=\\.)    means       lookbehind excluding "." and add
            2.)     .*          means       every character until
            3.)     $           meabs       end of string
            */
            
            String
                    regex = "(?<=\\.).*$",
                    input = "C:\\dir\\filename.ext";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
            String output = "no match";
            if (matcher.find()){
                output = matcher.group();
            }
            System.out.println("\t Output: " + output);
        }
    }
    This will output
    Java Code:
             Output: ext
    For help on Regex syntax see this link.

    This stuff is excellent! I hope that helped you. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  2. Doubt in simultaneous 'implementation' and 'extension'
    By ajaygargnsit in forum New To Java
    Replies: 2
    Last Post: 12-20-2007, 09:33 AM
  3. Regex pattern
    By ravian in forum New To Java
    Replies: 4
    Last Post: 12-11-2007, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •