Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-30-2008, 09:06 PM
Member
 
Join Date: Jan 2008
Posts: 17
gapper is on a distinguished road
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:

Code:
.[A-Za-z]*
Please advice. I would appreciate an early response to this.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-31-2008, 04:59 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
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
Code:
Output: ext
For help on Regex syntax see this link.

This stuff is excellent! I hope that helped you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make delete particular extension file from a directory Java Tip java.io 0 04-05-2008 11:13 AM
Eclipse Plugin Project - Extension Points JavaForums Java Blogs 0 01-28-2008 01:00 PM
Eclipse project - Extension page JavaForums Java Blogs 0 01-28-2008 01:00 PM
Doubt in simultaneous 'implementation' and 'extension' ajaygargnsit New To Java 2 12-20-2007 10:33 AM
Regex pattern ravian New To Java 4 12-11-2007 11:20 AM


All times are GMT +3. The time now is 08:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org