Solved

parsing a filename in java

Posted on 2004-09-23
6
188 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 46
split string containing \r\n in Java 46 38
Error trying to install RTMT Win7 5 41
Opening PDF on button click and fill new document 2 21
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

895 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

14 Experts available now in Live!

Get 1:1 Help Now