• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex to upload only csv files?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to build a regular expressions that will filter files and let upload only csv.
I am not the guru of regex, so maybe it would be simple for some of you?

Thanks in advance!
Pawel
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I'm assuming that you want all files that end with ".csv", correct?

The regex will match regular characters (that is, characters without a special meaning), so the end of the regex would be "csv".

A dot (.) has a special meaning so you have to escape it with a backslash (\.). But a backslash is a special character in Java Strings, so you have to escape the backslash too: "\\.csv".

Now you want to say, "Any number of characters before the .cvs". Dot (.) matches any character, but you want one or more characters. Plus (+) means, "At least one of the things just before me," so the regex would be ".+\\.csv".

Read the Java API on java.util.regex.Pattern and java.util.regex.Matcher for more information.
 
Pawel Kot
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick answer!

Exactly, I need every .csv file to be uploaded and all the others not to be.
The software I am using operates in Groovy.
I set the regular expression as: "^.*\.([cC][sS][vV]??)$" and it seems to be working, but I am aware that this is not perfect.
So, thank you for the precise regex and for your explanation.
You've made my day
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic