File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to specify multiple file suffices with regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to specify multiple file suffices with regex" Watch "How to specify multiple file suffices with regex" New topic
Author

How to specify multiple file suffices with regex

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

I would like to find out the correct regex to pickup multiple file suffices when looking for files that ends with “.txt”, “.xml” and “pdf” such as the following example:



Only the last ( v ) statement has worked by separate each suffix with its own endsWith() which means that there is something wrong with all my regex between ( i ) – ( iv ) .

Any idea on what is wrong with them?

Thanks in advance,

Jack
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

You can't use endsWith with regular expressions. Use matches instead:
The (?i) makes the matching case insensitive. Leave it out to have case sensitive matching. You need the leading .* because matches uses exact matching, not substring matching.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
  • Find lastIndexOf(".").
  • Add 1.
  • Substring from that position

  • ??
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7711
        
      20

    Campbell Ritchie wrote:
  • Find lastIndexOf(".").
  • Add 1.
  • Substring from that position
  • ??

    You still have the case-sensitivity problem that Rob mentioned.

    Rob Spoor wrote:...&& currentFile.getName().matches("(?i).*\\.(txt|xml|pdf)$"))

    Rob, do you know how that (?i) gets implemented? I can't see anything in the docs that says whether it's Unicode-aware or not.

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19682
        
      20

    For Unicode support there's two other flags: (?u) and (?U). See java.util.regex.Pattern, and more specifically UNICODE_CASE and UNICODE_CHARACTER_CLASS, for more information.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7711
        
      20

    Rob Spoor wrote:For Unicode support there's two other flags: (?u) and (?U). See java.util.regex.Pattern, and more specifically UNICODE_CASE and UNICODE_CHARACTER_CLASS, for more information.

    Ah, thanks a lot. I was looking in the wrong place.

    Winston
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19682
        
      20

    You're welcome.
    Jack Bush
    Ranch Hand

    Joined: Oct 20, 2006
    Posts: 235
    Hi Rob,

    Your initial suggestion below has worked like a gem:



    On the other hand, I don't understand what Campbell Ritchie's comment below mean:

    Find lastIndexOf(".").
    Add 1.
    Substring from that position


    Could someone elaborate a bit further on this?

    Thanks again,

    Jack
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19682
        
      20

    currentFile.getName().lastIndexOf('.') would return the index of the last dot, or -1 if there are no dots in the name. If you then add 1, you get the index of the first character after the last dot, or 0. Calling substring from that index on would give you either the extension, or the entire name.
    Jack Bush
    Ranch Hand

    Joined: Oct 20, 2006
    Posts: 235
    Crystal clear now Rob,

    Don't know what to do without you.

    Cheers,

    Jack
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19682
        
      20

    Get your answers from any other member around here?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38497
        
      23
    No, they didn’t
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to specify multiple file suffices with regex