Regular Expressions, Include & exclude files

Discussion in 'Java' started by andreasviklund@gmail.com, Jun 25, 2006.

  1. Guest

    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.
     
    , Jun 25, 2006
    #1
    1. Advertising

  2. Chris Guest

    wrote:
    > 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.
     
    Chris, Jun 25, 2006
    #2
    1. Advertising

  3. Alan Moore Guest

    On Sun, 25 Jun 2006 15:17:24 -0500, Chris <>
    wrote:

    > wrote:
    >> 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"
    >>
    >>

    >
    >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"
     
    Alan Moore, Jul 3, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    610
    Jay Douglas
    Aug 15, 2003
  2. KJ
    Replies:
    3
    Views:
    876
    Laurent Bugnion
    Nov 16, 2006
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    927
    Andreas Bogenberger
    Feb 22, 2008
  4. Henry Law
    Replies:
    3
    Views:
    959
    Henry Law
    Jul 22, 2004
  5. Noman Shapiro
    Replies:
    0
    Views:
    235
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page