首页 > 解决方案 > 正则表达式可选,在 primefaces 中使用 allowTypes

问题描述

这是我的正则表达式,在本地 java 中,它可以工作,但是当我试图将它放在 p:fileUpload allowTypes 上时它不起作用。

我的目标是 1) "itrs" 或 "ITRS" 是必须的 2) "-draft" 或 "-DRAFT" 是可选的 3) ".csv" 或 ".CSV" 是必须的

我想尽可能过滤文件名和文件扩展名

这适用于我的本地:(itrs|ITRS)((-draft|-DRAFT)?)(\.|\/)(csv|CSV)$

标签: javaregexprimefaces

解决方案


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 [.\/].


推荐阅读