• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to specify multiple file suffices with regex

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Find lastIndexOf(".").
  • Add 1.
  • Substring from that position

  • ??
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10509
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20610
    63
    Chrome Eclipse IDE Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 10509
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20610
    63
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome.
     
    Jack Bush
    Ranch Hand
    Posts: 235
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20610
    63
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 235
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Crystal clear now Rob,

    Don't know what to do without you.

    Cheers,

    Jack
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20610
    63
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Get your answers from any other member around here?
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49823
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, they didn’t
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic