1

here is my regex, in local java, it works, but when im trying to put it on p:fileUpload allowTypes it doesn't work.

my goals are 1) "itrs" or "ITRS" is a must 2) "-draft" or "-DRAFT" is optional 3) ".csv" or ".CSV" is a must

i want to filter the filename and file extension as much as possible

this is working on my local:(itrs|ITRS)((-draft|-DRAFT)?)(\.|\/)(csv|CSV)$

Applegate
  • 27
  • 5
  • Basically, you need to use the regex pattern within regex delimiters. See my answer below. In your requirements, you do not mention that there can be `/` before `csv`, hence I do not use it in the main regex, but I added a note at the bottom. – Wiktor Stribiżew Sep 19 '18 at 08:16

1 Answers1

1

You may use either

allowTypes="/^(?:itrs|ITRS)(?:-draft|-DRAFT)?\.(?:csv|CSV)$/"

or, if dRaFt and ItRS are also accepted, you may shorten the pattern a bit using i case insensitive modifier:

allowTypes="/^itrs(?:-draft)?\.csv$/i"

Note the use of / regex delimiters here. Also, see an example in PrimeFaces "FileUpload - Single" docs illustrating the use of regex delimiters.

NOTE: If you really need to match . or / before csv, replace \. with [.\/].

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • hi Wiktor, if it is okay with you, May you please explain what happen in your regex here in full details? including why do i have to note the use of "/" thanks in advance Wiktor. – Applegate Sep 19 '18 at 08:21
  • @Applegate That is *your* regex, I only added [**regex delimiters**](https://en.wikipedia.org/wiki/Regular_expression#Delimiters). `i` is a case insensitive [modifier](https://www.regular-expressions.info/modifiers.html) (as I already wrote). `(?:...)` are [non-capturing groups](https://stackoverflow.com/questions/3512471) (only used to group patterns, not to save and keep submatches). – Wiktor Stribiżew Sep 19 '18 at 08:25