Solved

parsing a filename in java

Posted on 2004-09-23
6
187 Views
Last Modified: 2010-03-31
I need to parse out a file name into different strings.

Filenames would include

NCWV1000245.jpg

I would need 1000245
0
Comment
Question by:vcgDevelopers
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
Comment Utility
String number = s.replaceAll("\\D", ""); // (where 's' is the filename )
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
String fileName = "NCWV1000245.jpg";
String base = fileName.substring(0, fileName.indexOf('.'));
String ext = fileName.substring( fileName.indexOf('.') );
0
 
LVL 3

Expert Comment

by:KeithWatson
Comment Utility
Assuming your filenames take the form: <some letters><some numbers>.<3 or 4 letters>

then this program will extract the numbers bit using a regular expression:

package com.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestRegExp {

      public static void main(String[] args) {
            
            Pattern pattern = Pattern.compile("^[A-Za-z]*([0-9]*)\\.[A-Za-z0-9]{3,4}$");
            
            String s = "NCWV1000245.jpg";

            Matcher m = pattern.matcher(s);
            
            if (m.matches()) {
                  String number = m.group(1);
                  System.out.println(number);
            }
     }

}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:KeithWatson
Comment Utility
That said, I've just become a fan of CEHJ's slimmer solution!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
LOL - i thought you might ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now