Java + regex how to check such string "LOAD_filesourceB-01012008_000058.dat" for type and number (last 6 digits)

how to implement such a requirement via regexp?

I have a list of filenames as String. 01012008-00001.dat-LOAD_filesourceA
LOAD_filesourceB-01012008-00001.dat
LOAD_filesourceB-01012008-00003.dat
LOAD_filesourceA-01012008-00004.dat
LOAD_filesourceA-01012008-000055.dat
LOAD_filesourceB-01012008_000055.dat
...
LOAD_filesourceB-01012008_000058.dat
etc. .d.

after downloading each file, this file is moved to the archive directory ... and I register the file type and download number (last 6 characters in the file name)
I have 2 pieces of information: 1- should the file I want to download be of type A or B 2 is the last loaded file number as a whole based on this I would like to get the file name of the next file ie the same file type and the download number (= last 6 digits before. ".dat") should be the next available number. say 12 is loaded, then I will search for 13 if 14, 15 is not available, etc. until I have processed all the files in that directory.

just a string like "LOAD_filesourceB-01012008_000058.dat" is given, I can check that it is file type B, and if the last loaded file number is 57 it satisfies requirement number 58. (> 57 I mean)

0


a source to share


3 answers


LOAD_filesource. (A | B) - [0-9] + - ([0-9]) + DAT



A or B will fall into group 1, file number in group 2. Then parse group 2 as a decimal integer.

+1


a source


See this:

public class Match {

    Pattern pattern = Pattern.compile("LOAD_filesource(A|B)-[0-9]{8}[_-]([0-9]{5,6})\\.dat");

    String files[] = {
        "LOAD_filesourceA-01012008-00001.dat",
        "LOAD_filesourceB-01012008-00001.dat",
        "LOAD_filesourceB-01012008-00003.dat",
        "LOAD_filesourceA-01012008-00004.dat",
        "LOAD_filesourceA-01012008-000055.dat",
        "LOAD_filesourceB-01012008_000055.dat",
        "LOAD_filesourceB-01012008_000058.dat"
    };

    public static void main(String[] args) {
        new Match().run();
    }

    private void run() {
        for (String file : files) {
            Matcher matcher = pattern.matcher(file);

            System.out.print(String.format("%s %b %s %s\n", file, matcher.matches(), matcher.group(1), matcher.group(2)));
        }
    }
}

      



with this output:

LOAD_filesourceA-01012008-00001.dat true A 00001
LOAD_filesourceB-01012008-00001.dat true B 00001
LOAD_filesourceB-01012008-00003.dat true B 00003
LOAD_filesourceA-01012008-00004.dat true A 00004
LOAD_filesourceA-01012008-000055.dat true A 000055
LOAD_filesourceB-01012008_000055.dat true B 000055
LOAD_filesourceB-01012008_000058.dat true B 000058

      

+1


a source


I don't know if this was intentional or not, but you have specified two different formats: one that uses a hyphen as the final delimiter and one that uses an underscore. If both are indeed supported, then you need:

LOAD_filesource(A|B)-[0-9]+[_-]([0-9])+.dat

      

Also, your six-digit number is sometimes five digits (e.g. 00001 in LOAD_filesourceA -...- 00001.dat), but the specified regex requires at least one digit.

Depending on how many files you are going to try to examine, you might be better off downloading a directory listing rather than randomly checking if the file exists. By using an appropriate comparison method, sorting your list can provide you with your files in a convenient order to work with.

0


a source







All Articles