Nayan Sonthalia Nayan Sonthalia - 1 year ago 99
Java Question

Regular expression and AwkFilenameFilter

We have a folder where we dump lot of files. Our program needs to read one of the specific files with the latest version. The file name would be something like "2016-03-04-12-46-48_ABC_123456_1.xml".

Insted of reading all the files and then iterating to find the exact file i have used following code with a regular expression

File folder = new File("C:\\some_folder")
folder.listFiles((FilenameFilter) new AwkFilenameFilter("(\\d){4}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}_ABC_" + <ID_String> +"_(\\d){1,2}"))

But for some reason the reqular expression is not working. Can someone please help with this?

Answer Source

It seems you are missing the file extension in the regex:

(\\d){4}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}_ABC_" + <ID_String> +"_(\\d){1,2}\\.xml
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download