parsing a filename in java

I need to parse out a file name into different strings.

Filenames would include

NCWV1000245.jpg

I would need 1000245
vcgDevelopersAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
String number = s.replaceAll("\\D", ""); // (where 's' is the filename )
0
 
zzynxSoftware engineerCommented:
String fileName = "NCWV1000245.jpg";
String base = fileName.substring(0, fileName.indexOf('.'));
String ext = fileName.substring( fileName.indexOf('.') );
0
 
KeithWatsonCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
KeithWatsonCommented:
That said, I've just become a fan of CEHJ's slimmer solution!
0
 
CEHJCommented:
LOL - i thought you might ;-)
0
 
CEHJCommented:
8-)
0
All Courses

From novice to tech pro — start learning today.