Regular Expressions, Include & exclude files

A

andreasviklund

Hi!

I have a javaprogram that should read xml-files from a directory. The
program could contain several types of files but it should only read
files with a certain pattern.

The file names will look like this:

"resultset_27_23.xml"

where the numbers will change, but the rest of the file name is the
same (resultset_XX_XX.xml).

But in the same directory it will also be files with the following
pattern:

"resultset_27_23_attachment1.xml"

Here, the numbers could change in the in the same way as the files
above, and the number after the text (attachment) could also differ
from file to file.Those files should not be read by the program.

I have tried to write a regular expression pattern that only reads the
first file types, and exlcudes the other ones, but it won´t work.

Does anyone have a solution to my problem? It is possible to use either
just one pattern, or two patterns; one for the files that should be
included, and one for the files that should be excluded.
 
C

Chris

Hi!

I have a javaprogram that should read xml-files from a directory. The
program could contain several types of files but it should only read
files with a certain pattern.

The file names will look like this:

"resultset_27_23.xml"

where the numbers will change, but the rest of the file name is the
same (resultset_XX_XX.xml).

But in the same directory it will also be files with the following
pattern:

"resultset_27_23_attachment1.xml"

Here, the numbers could change in the in the same way as the files
above, and the number after the text (attachment) could also differ
from file to file.Those files should not be read by the program.

I have tried to write a regular expression pattern that only reads the
first file types, and exlcudes the other ones, but it won´t work.

Does anyone have a solution to my problem? It is possible to use either
just one pattern, or two patterns; one for the files that should be
included, and one for the files that should be excluded.

I haven't tried it, but this may work:

"resultset_\\d\\d_\\d\\d\\.xml";

\d represents a single digit. \. escapes the dot. All backslashes are
doubled up so they are treated as literals in a Java string.
 
A

Alan Moore

I haven't tried it, but this may work:

"resultset_\\d\\d_\\d\\d\\.xml";

\d represents a single digit. \. escapes the dot. All backslashes are
doubled up so they are treated as literals in a Java string.

That doesn't cover the longer format. This one does:

"resultset_\\d\\d_\\d\\d(?:_attachment\\d)\\.xml"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top