Solved

parsing a filename in java

Posted on 2004-09-23
6
189 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
ID: 12136917
String number = s.replaceAll("\\D", ""); // (where 's' is the filename )
0
 
LVL 37

Expert Comment

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

Expert Comment

by:KeithWatson
ID: 12136997
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 3

Expert Comment

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

Expert Comment

by:CEHJ
ID: 12137045
LOL - i thought you might ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12137276
8-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 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