How to validate a filename using Regex?

user3007165 :

Hi i am writing a regex for filename validation. Combining bits and pieces from different forums but not having the correct test results. Below is the format with the example that i want to achieve

FORMAT: ABC<1|2>_DEF<6-7digits>_GHI<1|2|3>.xml EXAMPLE: ABC2_DEF1234567_GHI3.xml

and here is the regex defined in Java.

private static final String PATTERN = "ABC[1|2|3]+_DEF\\d[0-9]{6,7}+_GHI[1|2|3].xml$";
CinCout :

The following should suffice:

"(?i)abc[12]_def\d{6,7}_ghi[123]\.xml

Note the use of ?i flag to match case-insensitively. You may also use the Pattern.CASE_INSENSITIVE constant.

This regex matches abc followed by either a 1 or a 2, followed by _def, followed by 6-7 digits, followed by _ghi, followed by a 1, 2, or 3, with the extension .xml.

Demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=147181&siteId=1